1
import random

n = int (input('guess the number'))

randomnumber = random.randint(1,100)

while True:
    if n == randomnumber:
        print ('you have won')
        break   
    elif randomnumber > n :
        print('you guessed too high')
        
    else :
        print ('you guessed too low')
        
 
print ('thank you for playing')
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • 1
    Don't you need to read a new number from the user on each iteration of the loop? As it stands, they only get one guess — there's about a 1% chance that they guessed correctly. – Jonathan Leffler Aug 30 '20 at 02:34

4 Answers4

3

The loop never ends because neither n nor randomnumber gets updated inside the loop. Try to insert n = int (input('guess the number')) inside the loop.

Willy satrio nugroho
  • 908
  • 1
  • 16
  • 27
SHINJI.K
  • 308
  • 1
  • 6
1

Any Python program can be broken if you press Control + C. Furthermore, I think what you want is that the program should ask the user every time they get it wrong. So, at the end of the while loop, repeat the line of code asking the user for the value of n.

Like Jonathan said, it would be a better idea to move the input statement to the top of the loop and remove the first input statement at the top of the program.

import random

randomnumber = random.randint(1,100)

while True:
    n = int (input('guess the number'))
    if n == randomnumber:
        print ('you have won')
        break   
    elif randomnumber > n :
        print('you guessed too high')
        
    else :
        print ('you guessed too low')
        
 
print ('thank you for playing')
Dharman
  • 30,962
  • 25
  • 85
  • 135
Safwan Samsudeen
  • 1,645
  • 1
  • 10
  • 25
-1

It repeats itself because it is in a 'while true' is a loop and it will repeat itself forever to solve it remove the while true and break statments hope that helped!

lukes
  • 1
  • I think that analysis misses the problem. – Jonathan Leffler Aug 30 '20 at 02:36
  • check my other comment i have the code that should fix it! – lukes Aug 30 '20 at 02:44
  • Don't write two answers — edit one answer to add extra information. Your new answer needs some explanation. You should delete one of your answers and edit the relevant information into (and out of) the other. —— Also note that on SO, these messages are "comments". This is a comment to an answer. What you wrote first is an answer — and answers are not comments. If an 'answer' isn't actually answering the question, it will be flagged as 'not an answer' and may be down-voted. – Jonathan Leffler Aug 30 '20 at 02:47
-1


import random

n = int (input('guess the number'))

randomnumber = random.randint(1,100)

if n == randomnumber:
    print ('you have won')
elif randomnumber > n :
    print('you guessed too high')
        
else:
    print ('you guessed too low')
        
print ('thank you for playing')

lukes
  • 1