我是python的新手,到目前为止,我的课程看起来像是:
if choice=="1":
def addition():
print("You are doing addition. X+Y=Z")
X = int(input("Enter X:"))
Y = int(input("Enter Y:"))
sum = X + Y
print ("your total is:")
print (sum)
Well = "y"
while Well == "y":
again = input("Would you like to go again? (y/n)")
if again == "y":
addition()
if again == "n":
project()
else:
print("sorry re-enter choice")
它询问我是否要在显示实际的添加部分之前再次去。我该如何解决?谢谢你的帮助:)
答案 0 :(得分:2)
很多方法,但您可以将addition()
功能置于顶部,如下所示:
Well = "y"
while Well == "y":
addition()
again = input("Would you like to go again? (y/n)")
if again == "n":
project()
if again not in ("n", "y"):
print("sorry re-enter choice")
答案 1 :(得分:1)
只需在首次运行时添加一个从false
更改为true
的标记,这样您就不会在首次运行时看到“再次”语句。
答案 2 :(得分:1)
几点:
sum
用作变量名称。它是内置的Python。答案 3 :(得分:1)
我认为在你做完你的事情之后,问你是否想要在循环结束时再去一次会更有意义:
while loop_condition:
#do_your_stuff:
bla bla bla
#see if you should go again
bla bla = raw_input("Do you want to go again")
事实上,99%的时间你的while循环将遵循这种模式(检查条件,运行循环体,更新条件),如果你以不同的顺序做事(比如在运行体之前更新条件)它通常是你可能做错事的标志。