简单python程序的问题
我有一个程序来测试行星离太阳有多远。唯一的问题是,无论我给出什么样的答案,结果总是正确的。以下是我的代码链接: 如果可能的话,我想要一个更简单的答案,因为我还不太精通python 有关守则:简单python程序的问题,python,windows,string,if-statement,boolean,Python,Windows,String,If Statement,Boolean,我有一个程序来测试行星离太阳有多远。唯一的问题是,无论我给出什么样的答案,结果总是正确的。以下是我的代码链接: 如果可能的话,我想要一个更简单的答案,因为我还不太精通python 有关守则: mercury = "57.9" mercury2 = "57900000" def Mercury(): ans = raw_input("How far is Mercury from the sun? ") if mercury or mercury2 in ans:
mercury = "57.9"
mercury2 = "57900000"
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
问题是你有:
if mercury or mercury2 in ans:
如果mercury
的计算结果为True
(通常是这样),或者ans中的mercury2为True
,则此if语句将为True
mercury
是一个非空字符串(mercury=“57.9”
),其计算结果为True
。例如,尝试bool(“57.9”)
查看Python总是为非空字符串计算True
。如果字符串为空,则它将为False
因此,无论用户回答什么,您的代码都会说它是正确的。以下是您可以写的内容:
if mercury in ans or mercury2 in ans:
但最好还是写下来(见下面评论中的讨论):
问题是你有:
if mercury or mercury2 in ans:
如果mercury
的计算结果为True
(通常是这样),或者ans中的mercury2为True
,则此if语句将为True
mercury
是一个非空字符串(mercury=“57.9”
),其计算结果为True
。例如,尝试bool(“57.9”)
查看Python总是为非空字符串计算True
。如果字符串为空,则它将为False
因此,无论用户回答什么,您的代码都会说它是正确的。以下是您可以写的内容:
if mercury in ans or mercury2 in ans:
但最好还是写下来(见下面评论中的讨论):
问题在于if语句的条件
例如:
if ans == venus or venus2:
这应该是:
if ans == venus or ans == venus2:
问题在于if语句的条件
例如:
if ans == venus or venus2:
这应该是:
if ans == venus or ans == venus2:
你有这个:
if mercury or mercury2 in ans:
与此相反:
if ans in (mercury, mercury2):
但是,您有一个更深层次的问题。代码是这样的
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
def main():
while True:
print "Planetary Distance from the Sun"
time.sleep(.5)
rand = random.randint(1,1)
if rand==1:
ask_planet_distance("Mercury", mercury, mercury2)
elif rand==2:
ask_planet_distance("Venus", venus, venus2)
...
def ask_planet_distance(planet_name, distance1, distance2):
while True:
ans = raw_input("How far is {} from the sun? ".format(planet_name))
if ans in (distance1, distance2):
break
else:
print "Incorrect!"
print "Correct!"
time.sleep(.5)
os.system("cls")
最终将导致堆栈溢出。这是因为您正在调用函数,但从未从中返回强>
您应该重新构造代码以使用循环
你也应该考虑删除程序
中的一些重复。
你可以使用这样的函数
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
def main():
while True:
print "Planetary Distance from the Sun"
time.sleep(.5)
rand = random.randint(1,1)
if rand==1:
ask_planet_distance("Mercury", mercury, mercury2)
elif rand==2:
ask_planet_distance("Venus", venus, venus2)
...
def ask_planet_distance(planet_name, distance1, distance2):
while True:
ans = raw_input("How far is {} from the sun? ".format(planet_name))
if ans in (distance1, distance2):
break
else:
print "Incorrect!"
print "Correct!"
time.sleep(.5)
os.system("cls")
通过将行星数据存储在列表中,您可以更进一步,您有:
if mercury or mercury2 in ans:
与此相反:
if ans in (mercury, mercury2):
但是,您有一个更深层次的问题。代码是这样的
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
def main():
while True:
print "Planetary Distance from the Sun"
time.sleep(.5)
rand = random.randint(1,1)
if rand==1:
ask_planet_distance("Mercury", mercury, mercury2)
elif rand==2:
ask_planet_distance("Venus", venus, venus2)
...
def ask_planet_distance(planet_name, distance1, distance2):
while True:
ans = raw_input("How far is {} from the sun? ".format(planet_name))
if ans in (distance1, distance2):
break
else:
print "Incorrect!"
print "Correct!"
time.sleep(.5)
os.system("cls")
最终将导致堆栈溢出。这是因为您正在调用函数,但从未从中返回强>
您应该重新构造代码以使用循环
你也应该考虑删除程序
中的一些重复。
你可以使用这样的函数
def Mercury():
ans = raw_input("How far is Mercury from the sun? ")
if mercury or mercury2 in ans:
print "Correct!"
time.sleep(.5)
os.system("cls")
main()
else:
print "Incorrect!"
Mercury()
def main():
while True:
print "Planetary Distance from the Sun"
time.sleep(.5)
rand = random.randint(1,1)
if rand==1:
ask_planet_distance("Mercury", mercury, mercury2)
elif rand==2:
ask_planet_distance("Venus", venus, venus2)
...
def ask_planet_distance(planet_name, distance1, distance2):
while True:
ans = raw_input("How far is {} from the sun? ".format(planet_name))
if ans in (distance1, distance2):
break
else:
print "Incorrect!"
print "Correct!"
time.sleep(.5)
os.system("cls")
你可以更进一步,将行星数据存储在列表中
有150行代码,你能缩小问题的范围,然后在这里发布相关的代码吗?(这一过程也将加深您对代码的理解)共有150行代码,您能否缩小问题范围,并在此处发布相关代码?(这一过程也将加深您对代码的理解)或者如果[mercury,mercury2]中有ans,则可能是。
@Deestan:这也会起作用,但这确实意味着用户必须只回答一个数字。如果他们回答“57900000(大致)”
,那就错了。是的,但另一种选择有误报。“34539485934557.99”将被认为是正确的。@Deestan:好的,那更糟。我会更新我的答案。谢谢,我的代码现在运行得更好了!现在回到记忆ESRT…或者如果[mercury,mercury2]
@Deestan中有ans,则可能是:这也会起作用,但这确实意味着用户只能用一个数字回答。如果他们回答“57900000(大致)”
,那就错了。是的,但另一种选择有误报。“34539485934557.99”将被认为是正确的。@Deestan:好的,那更糟。我会更新我的答案。谢谢,我的代码现在运行得更好了!现在回到记忆ESRT…+1,用于从Mercury()返回的非常重要的观察值。
+1用于从Mercury()返回的非常重要的观察值。