76.0k views
2 votes
Write a code in python that guesses a hardcoded answer and keeps on asking the user until the user gets the answer correct. The cmputer should be telling the user if the number they are guessing is too low or too high.

User Wallop
by
6.9k points

1 Answer

1 vote

import random

#You can change the range.

answer = random.randint(1,1000)

counter = 0

while(True):

guess = int(input("Make a guess: "))

message = "Too high!" if guess>answer else "Too low!" if guess<answer else "You won!"

print(message)

if(message=="You won!"):

print("It took",counter,"times.")

break

else:

counter+=1

Write a code in python that guesses a hardcoded answer and keeps on asking the user-example-1
User Zlandorf
by
6.7k points