Home > Blockchain >  How can I make my program repeat execute many times?
How can I make my program repeat execute many times?

Time:10-11

I have a program that works fine, however I need to make it so that it can execute again when the if statement regarding playing again is satisfied.

import random
n=random.randint(0,10)
print(n)
number= int(input('Guess what the number is'))
count=0
while number !=n:
    count=count 1
    number= int(input('Guess what the number is'))
    
    if number< n:
     print("that is too low") 
     

    elif number>n:
      print("That is too high")
      
      
      
else:
    print("You got it right in"  " " str(count 1) " "  "tries")
    print(count 1)
    yesorno= str(input('Do you want to play again? y or n'))
    if yesorno=="y":
        number= int(input('Guess what the number is'))
        

    elif yesorno=="n":
        print("Goodbye")
            

CodePudding user response:

If you don't want an ugly big while loop, use functions. It makes your code cleaner.

import random

def play():
    input("Guess a number between 1 and 10: ")
    random_number = random.randint(1, 10)
    guess = None
    attempts = 0
    while guess != random_number:
        guess = int(input("Pick a number from 1 to 10: "))
        attempts  = 1
        if guess < random_number:
            print("TOO LOW!")
        elif guess > random_number:
            print("TOO HIGH!")
    print("YOU GOT IT! The number was {}, you got it in {} attempts.".format(random_number, attempts))

def main():
    play()
    while input("Play again? (y/n) ").lower() != "n":
        play()

main() # Call the main function

CodePudding user response:

import random

n=random.randint(0,10)
count = 0
while True:
    count=count 1
    number= int(input('Guess what the number is'))
    
    if number< n:
        print("that is too low") 
    elif number>n:
        print("That is too high")
    else:
        print("You got it right in"  " " str(count) " "  "tries")
        print(count)
        yesorno= str(input('Do you want to play again? y or n'))
        if yesorno=="y":
            n=random.randint(0,10)
            count = 0
        elif yesorno=="n":
            print("Goodbye")
            break
            

CodePudding user response:

import random
n=random.randint(0,10)
print(n)

count=0
while True:
    count=count 1
    number= int(input('Guess what the number is '))    
    if number< n:
        print("that is too low")
    elif number>n:
        print("That is too high")
    elif number == n:
        print("You got it right in"  " " str(count 1) " "  "tries")
        print(count 1)
        yesorno= str(input('Do you want to play again? y or n'))
        if yesorno=="n":
            print("Goodbye")
            break

Use a while loop with a condition that will always be true, like while True:. To stop this infinite loop, use the break statement within the while loop. If user inputs "y", the loop will continue because it has not been told the break.

  • Related