笔记17 笨办法中的习题26 更多更多的检查和耐心
终于算是学完了练习26,整本书的半部给折腾完了,好像有了一点对于python语言的感觉。不过仅仅只是感觉而已。
按教材的要求下载了这个练习的脚本,总共查出了21个错误。这个脚本应该是前面练习的一个集成,有前面的练习做过的经历,这个习题虽然魔人,但难度不是很大,只要反复去对照清理,做出来还是有希望的。虽然全部打开代码这个过程还没有走完,再琢磨一下,大概应该也能做到,先把查出的问题做个笔记吧。
练习ex26分成几乎可以不相干的四个部分,代码可以这样做,大概是和自然语言有差别的地方。我一个部分,一个部分地分别给出代码和查出的结果。
第一个部分是一段关于年龄身高和体重的代码,有2个错。
原代码
在这里插入代码片print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
print("How much do you weigh?", end=' '
weight = input()
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
改后代码
在这里插入代码片print("How old are you?", end=' ') #第一段
age = input()
print("How tall are you?", end=' ')
height = input()
print("How much do you weigh?", end=' ')
weight = input()
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
在这里插入代码片
在ps执行情况
在这里插入代码片PS C:\Users\lenovo> cd 1pythonw
PS C:\Users\lenovo\1pythonw> python ex26.py
C:\Users\lenovo\AppData\Local\Programs\Python\Python38\python.exe: can't open file 'ex26.py': [Errno 2] No such file or directory
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 5
weight = input()
^
SyntaxError: invalid syntax
第二部分是一段函数,有7个错
原代码在这里插入代码片
script, filename = argv
txt = open(filenme)
print(“Here’s your file {filename}:”)
print(tx.read())
print(“Type the filename again:”)
file_again = input("> ")
txt_again = open(file_again)
print(txt_again_read())
print(‘Let’s practice everything.’)
print('You’d need to know ‘bout escapes
with \ that do \n newlines and \t tabs.’)
poem = “”"
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
“”"
print("--------------)
print(poem)
print(--------------")
改后代码`在这里插入代码片script, filename = argv
txt = open(filename)
print(“Here’s your file {filename}:”)
print(txt.read())
print(“Type the filename again:”)
file_again = input(">")
txt_again = open(file_again)
print(txt_again_read())
print(‘Let’s practice everything.’)
print(‘You’d need to know ‘bout escapes with \ that do:’)
print(’\n newlines and \t tabs.’)
poem = “”"
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explanation
\n\t\twhere there is none.
“”"
print("--------------")
print(poem)
print("--------------")
`
在ps执行情况
在这里插入代码片SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 24
print('Let's practice everything.')
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 24
print('Let's practice everything.')
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 25
print('You\'d need to know \'bout escapes
^
SyntaxError: EOL while scanning string literal
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 25
print('You\'d need to know \'bout escapes
^
SyntaxError: EOL while scanning string literal
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 37
print("--------------)
^
SyntaxError: EOL while scanning string literal
PS C:\Users\lenovo\1pythonw> python ex26.py
File "ex26.py", line 42
five = 10 - 2 + 3 -
第三部分是一个计算,有6个错
原代码`在这里插入代码片five = 10 - 2 + 3 -
print(f"This should be five: {five}"
def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars = secret_formula(start_point)
remember that this is another way to format a string
print(“With a starting point of: {}”.format(start_point))
it’s just like with an f"" string
print(f"We’d have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
print(“We can also do that this way:”)
formula = secret_formula(startpoint)
this is an easy way to apply a list to a format string
print(“We’d have {} beans, {} jars, and {} crates.”.format(*formula))`
改后代码在这里插入代码片
five = 10 - 2 + 3 - 6
print(f"This should be five: {five}")
def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
start_point = 10000
beans, jars, crates = secret_formula(start_point)
remember that this is another way to format a string
print(“With a starting point of: {}”.format(start_point))
it’s just like with an f"" string
print(f"We’d have {beans} beans, {jars} jars, and {crates} crates.")
start_point = start_point / 10
print(“We can also do that this way:”)
formula = secret_formula(start_point)
this is an easy way to apply a list to a format string
print(“We’d have {} beans, {} jars, and {} crates.”.format(*formula))
在ps执行情况
在这里插入代码片PS C:\Users\lenovo\1pythonw> python ex26.1.py exam
File "ex26.1.py", line 45
def secret_formula(started):
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File "ex26.2.py", line 6
weight = input()
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File "ex26.2.py", line 6
weight = input()
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File "ex26.2.py", line 44
print(f"This should be five: {five}")
第四部分是另一个计算,有6个错
原代码`在这里插入代码片eople = 20
cates = 30
dogs = 15
if people < cats:
print “Too many cats! The world is doomed!”
if people < cats:
print(“Not many cats! The world is saved!”)
if people < dogs:
print(“The world is drooled on!”)
if people > dogs
print(“The world is dry!”)
dogs += 5
if people >= dogs:
print(“People are greater than or equal to dogs.”)
if people <= dogs
print("People are less than or equal to dogs.)
if people = dogs:
print(“People are dogs.”)
改后代码在这里插入代码片
# 第四段计算
people = 20
cates = 30
dogs = 15
if people < cats:
print(“Too many cats! The world is doomed!”)
if people > cats:
print(“Not many cats! The world is saved!”)
if people < dogs:
print(“The world is drooled on!”)
if people > dogs:
print(“The world is dry!”)
dogs += 5
if people >= dogs:
print(“People are greater than or equal to dogs.”)
if people <= dogs:
print(“People are less than or equal to dogs.”)
if people == dogs:
print(“People are dogs.”)
在ps执行情况在这里插入代码片
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 84
if people > dogs
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 84
if people > dogs
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 84
if people > dogs
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 93
if people <= dogs
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 93
if people <= dogs
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 94
print("People are less than or equal to dogs.)
^
SyntaxError: EOL while scanning string literal
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 94
print("People are less than or equal to dogs.)
^
SyntaxError: EOL while scanning string literal
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 97
if people = dogs:
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
File “ex26.2.py”, line 97
if people = dogs:
^
SyntaxError: invalid syntax
PS C:\Users\lenovo\1pythonw> python ex26.2.py
How old are you? 70
How tall are you? 170
How much do you weigh? 60
So, you’re 70 old, 170 tall and 60 heavy.
Traceback (most recent call last):
File “ex26.2.py”, line 10, in
script, filename = argv
NameError: name ‘argv’ is not defined
PS C:\Users\lenovo\1pythonw> python ex26.2.py exam.txt
How old are you? 70
How tall are you? 170
How much do you weigh? 60
So, you’re 70 old, 170 tall and 60 heavy.
Traceback (most recent call last):
File “ex26.2.py”, line 10, in
script, filename = argv
NameError: name ‘argv’ is not defined
PS C:\Users\lenovo\1pythonw>
2020/04/18