基本上我对python很新,所以我决定制作一个简单的计算器,我已经完成了计算器的编码,一切正常,我很高兴,但是我想要一个if-else语句,看看他们是否我想继续另一个计算。所以这是我的代码的顶部和我的代码的底部部分,我想知道如何获得它,以便在代码的'else'部分之后,它只运行其余的代码。
import os
done = ("")
if done == True:
os._exit(0)
else:
print ("---CALCULATOR---")
...
done = str(input("Would you like to do another calculation? (Y/N) "))
if done == "N" or "n":
done = True
if done == "Y" or "y":
done = False
任何帮助都将不胜感激。
答案 0 :(得分:2)
你会想要这样的东西......
import os
done = False
while not done:
print ("---CALCULATOR---")
...
# be careful with the following lines, they won't actually do what you expect
done = str(input("Would you like to do another calculation? (Y/N) "))
if done == "N" or "n":
done = True
if done == "Y" or "y":
done = False
答案 1 :(得分:2)
if done == "N" or "n":
以上条件会检查done == "N"
或"n"
。这将始终评估为True
,因为在Python中,非空字符串的计算结果为布尔True
。
正如评论中所建议的那样,你应该使用while循环让程序继续执行,直到用户键入“N”或“n”。
import os
finished = False
while not finished:
print ("---CALCULATOR---")
...
done = str(input("Would you like to do another calculation? (Y/N) "))
if done == "N" or done == "n":
finished = True