Bananiel - 6 months ago 21
Python Question

# Break out of if while loop in python

I made this counting system in Python 3, https://repl.it/KUG5/1. It's purpose is to choose either addition, substraction, multiplication or division. While also choosing a score system, where the point is to reach the maximum score.

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.")

The code above has just the addition part, but my question is: How can I return it to
Valg = int(input("\n Addition (1), Subtraction (2), Multiplication (3), Division (4): "))
after the user has succeeded in their choice?

Answer Source
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.")

Just type while true

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download