Ask coding questions

← Back to all posts
How do you loop?
MisterMonkey (1)

I want to loop this. How do you do it?

Comments
hotnewtop
[deleted]

Add for i in range(100):
So your code would be:

import random
for i in range(100):
  print ("Guess the number 1-10!")
  n = random.randint(1, 1)
  question = input("What is your guess?: ")
  if question == (n):
    print("You are correct!")
  else:
    print("The answer was:")
    print(n)

You can customize the hundred to be how much times you want the loop to loop. Or you can let the user choose themselves

import random
userInput = int(input("How much times do you want to play guess the number?"))
for i in range(userInput):
  print ("Guess the number 1-10!")
  n = random.randint(1, 1)
  question = input("What is your guess?: ")
  if question == (n):
    print("You are correct!")
  else:
    print("The answer was:")
    print(n)
[deleted]

@MisterMonkey Yes your welcome, if this helped mark me as correct

ZoutigeWolf (2)

You could do
while True:
so it keeps restarting until you stop the code