skip to Main Content
import random

chance = 0

random_integer = random.randint(0, 10)

while chance <= 2:
    n = int(input("Guess the number between 1 to 10:"))
    
    if (n < 0 or n > 10):
        print("number out of range")
        chance+=1
        
    else:
         # Generate a random integer between 0 and 10 (inclusive)
        if (n == random_integer):
            print("You Win")
            break
            
        elif (n > random_integer):
            print("number guessed is greater than the lucky number")
            chance +=1
            
        elif (n < random_integer):
            print("number guessed is less than the lucky number")
            chance+=1

print("Lucky number was:",random_integer)

I tried installing the latest python version and changing the python interpreter path to new one, but it’s still not working.

2

Answers


  1. Ensure you used proper indentation??

    Login or Signup to reply.
  2. check this code

    import random
    
    chance = 0
    
    random_integer = random.randint(0, 10)
    
    n = int(input("Guess the number between 1 to 10:"))
    
    while chance <= 2: 
    
        if (n < 0 or n > 10):
                print("number out of range")
                chance+=1
                
        else:
                 # Generate a random integer between 0 and 10 (inclusive)
                if (n == random_integer):
                    print("You Win")
                    break
                    
                elif (n > random_integer):
                    print("number guessed is greater than the lucky number")
                    chance +=1
                    
                elif (n < random_integer):
                    print("number guessed is less than the lucky number")
                    chance+=1
    print("Lucky number was:",random_integer)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search