[英]Break out of if while loop in python
我使用Python 3 https://repl.it/KUG5/1創建了這個計數系統。 目的是選擇加,減,乘或除。 在選擇分數系統的同時,要達到最高分數。
import random
import time
Valg = int(input("\n Addition (1), Subtraction (2), Multiplication (3), Division (4): "))
PoengValgMin = -int(input("\n Choose a minimum score: "))
PoengValgMax = int(input("\n Choose a maximum score: "))
if Valg == 1:
BrukerPoeng = 0
Runder = 0
while BrukerPoeng < PoengValgMax:
Runder = Runder + 1
t_start = time.time()
x = random.randint(5,12)
y = random.randint(5,12)
print("\n What is", x, "+", y, "equal to?")
Svar = int(input("\n Answer here: "))
if Svar == (x+y):
BrukerPoeng = BrukerPoeng + 1
print("\n you are correct, you have now", BrukerPoeng, "points.")
else:
BrukerPoeng = BrukerPoeng - 1
print("\n You were wrong! You have now", BrukerPoeng, "points. The correct answer was", (x+y))
if BrukerPoeng == PoengValgMin:
BrukerPoeng = BrukerPoeng + PoengValgMax
print("You have too many incorrect answers and will be reset.")
t_slutt = time.time()
t_tid = t_slutt - t_start
print("\n Congratulations! You have now", BrukerPoeng, "points, which you used", Runder, "rounds to complete. The time you used was", round(t_tid,2), "seconds.")
上面的代碼只有加法部分,但是我的問題是:如何將其返回給Valg = int(input("\\n Addition (1), Subtraction (2), Multiplication (3), Division (4): "))
用戶成功選擇之后?
一個例子:
def myfunc():
# put code here
return # add return after print or wherever you want to escape
while 1:
myfunc()
import random
import time
while True:
Valg = int(input("\n Addition (1), Subtraction (2), Multiplication (3), Division (4): "))
PoengValgMin = -int(input("\n Choose a minimum score: "))
PoengValgMax = int(input("\n Choose a maximum score: "))
if Valg == 1:
BrukerPoeng = 0
Runder = 0
while BrukerPoeng < PoengValgMax:
Runder = Runder + 1
t_start = time.time()
x = random.randint(5,12)
y = random.randint(5,12)
print("\n What is", x, "+", y, "equal to?")
Svar = int(input("\n Answer here: "))
if Svar == (x+y):
BrukerPoeng = BrukerPoeng + 1
print("\n you are correct, you have now", BrukerPoeng, "points.")
else:
BrukerPoeng = BrukerPoeng - 1
print("\n You were wrong! You have now", BrukerPoeng, "points. The correct answer was", (x+y))
if BrukerPoeng == PoengValgMin:
BrukerPoeng = BrukerPoeng + PoengValgMax
print("You have too many incorrect answers and will be reset.")
t_slutt = time.time()
t_tid = t_slutt - t_start
print("\n Congratulations! You have now", BrukerPoeng, "points, which you used", Runder, "rounds to complete. The time you used was", round(t_tid,2), "seconds.")
只要輸入while true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.