笨方法”学Python3,习题 26:恭喜你,现在可以考试了!
# 考试前需要下载错误脚本,打开下方链接复制到新脚本即可
一、考试
错误的脚本ex26.py:
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.")
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(--------------")
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))
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.")
---------------------------------------------------------------------------------------------------
更改后的:
print("How old are you?", end=' ')
age = input()
print("How tall are you?", end=' ')
height = input() # 没定义变量height
print("How much do you weigh?", end=' ') # 缺少括号
weight = input()
print(f"So, you're {age} old, {height} tall and {weight} heavy.")
from sys import argv # 没有导入argv
script, filename = argv
txt = open(filename) # 变量打错了
print(f"Here's your file {filename}:") # 格式化字符串少了f
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 \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("--------------") # 少了双引号
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))
people = 20
cats = 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 D:\代码> & D:/软件/python/python.exe d:/代码/ex26.py txtx.txt
How old are you? 19
How tall are you? 197
How much do you weigh? 83
So, you're 19 old, 197 tall and 83 heavy.
Here's your file txtx.txt:
222222222222
555555555555
Type the filename again:
> txtx.txt
222222222222
555555555555
Let's practice everything.
'You'd need to know 'bout escapes
with \ that do
newlines and tabs.'
--------------
The lovely world
with logic so firmly planted
cannot discern
the needs of love
nor comprehend passion from intuition
and requires an explanation
where there is none.
--------------
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
Too many cats! The world is doomed!
The world is dry!
where there is none.
--------------
This should be five: 5
With a starting point of: 10000
We'd have 5000000 beans, 5000.0 jars, and 50.0 crates.
We can also do that this way:
We'd have 500000.0 beans, 500.0 jars, and 5.0 crates.
Too many cats! The world is doomed!
The world is dry!
People are greater than or equal to dogs.更改后的p1更改后的p2运行结果p3运行结果p4
总结:
1、定义函数不要缺封号
2、变量名不要打错
3、记得要导入特征
4、判断是否等于用==
5、仔细检查
^ v ^,知乎此系列文章内容均会在微信公众号中同步更新,公众号:小民有个小旮旯