在python中打破if while循环

时间:2017-08-22 10:42:21

标签: python python-3.x if-statement while-loop

我在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): "))

2 个答案:

答案 0 :(得分:1)

一个例子:

def myfunc():
    # put code here
    return # add return after print or wherever you want to escape


while 1:
    myfunc()

答案 1 :(得分:0)

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

即可