Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is wrong with my code? It just randomly stops

This is the code, but I see no mistake. It is supposed to be a simple guessing game.

import random
a=random. randint(1, 10)
b=random. randint(11, 20)
guess_count=0
print('Hello, pick a number from '+str(a)+' '+'to '+str(b)+'.')
number=int(input())
guess=random. randint(a, b)
while number < guess:
    print('Number too low. Guess again: ')
    number=int(input())
    guess_count+=1
while number > guess:
    print('Number too high. Guess again: ')
    number=int(input())
    guess_count+=1
if number == guess:
    print('Nice, you guessed right!')
    print('It took you '+str(guess_count)+' attempts.')

I tried some methods my teacher showed me but none of them worked. I expect it to go on forever until you don’t guess the right number.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This maybe what you intend:

import random
a=random. randint(1, 10)
b=random. randint(11, 20)
guess_count=0
print('Ahoj, zadaj cislo od '+str(a)+' '+'do '+str(b)+'.')
number=int(input('Guess a number: '))
guess=random. randint(a, b)

while number < guess or number > guess:
    guess_count+=1
    if number < guess:
        print('Number too low. Guess again: ')
    else:
        print('Number too high. Guess again: ')
    number=int(input('Guess a number: '))

print(f"Nice, you guessed right! It took you {guess_count} attempts.")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading