0

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?

3
  • How about putting it inside a function? If succeed return a value and repeat. Commented Aug 22, 2017 at 10:44
  • Put it all in a loop...? Commented Aug 22, 2017 at 10:45
  • You can use another while loop which contains almost everything in your code. If you want it to run forever, use while True. Remember you can use break to break out of a loop. Commented Aug 22, 2017 at 10:46

2 Answers 2

1

One example:

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


while 1:
    myfunc()
Sign up to request clarification or add additional context in comments.

Comments

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

Just type while true

1 Comment

Thank you everyone :) I went with the one from @keipa, while True: worked perfect!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.