简体   繁体   中英

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?

One example:

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

Just type while true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM