How do I stop this from repeating?


I am new to coding. It works alright until you guess a number. then it says either higher or lower eternally. Please help me understand what I’ve done wrong. I have tried searching the internet and trying to retype some of the code but it won’t work. I am trying to make a conversation as well as a mini guess the number game.

Here’s my Code

# Conversation A

import random

print("Answer all questions with 'yes' or 'no' and press ENTER")   

print('Hello I am Jim the computer')   
z = input('How are you? ')    
if z == 'Good' or z == 'Great' or z == 'good' or z == 'great':    
    print("That's great!")    
    print("Oh. That's sad. I hope you feel better.")    
a = input("what's your name? ")    
print(a + "? Thats a nice name.")    
b = input('Do you own any pets? ')    
if b == 'yes' or b == 'Yes':    
    print("That's cool. I love pets!")    
    print("That's a shame, you should get a pet. ")    
c = input("Do you like animals? ")    
if c == 'yes' or c == 'Yes':
    print("Great! Me too!")
    print("Oh that's sad. They're really cute. ")

d = input("Do you want to see a magic trick? ")
if d == "yes" or d == "Yes":
    input("Ok! Think of a number between 1-30. Do you have it? ")
    print("Double that number.")
    print("Add ten.")
    print("Now divide by 2...")
    print("And subtract your original number!")
    y = input("Was your answer 5? ")
    if y == 'yes' or y == 'Yes':
        print("Yay i got it right! I hope you like my magic trick.")
        print("Oh that's sad. I'll work on it. I really like magic tricks and games.")
    print("Ok. Maybe next time. I love magic tricks and games!")

e = input("Do you want to play a game with me? ")
if e == "yes" or e == "Yes":
    randnum = random.randint(1,100)
    print("I am thinking of a number between 1 and 100...")

if e == 'no' or e == 'No':
    print("oh well see you next time" + a + '.')

guess = int(input())

while guess is not randnum:
    if guess == randnum:
        print("Nice guess " + a + "! Bye, have a nice day!")
    if guess < randnum:
    if guess > randnum:


You need to add a break statement when you want stop looping and move the input for guess inside the loop so you don’t exit before printing the statement:

while True:
    guess = int(input())
    if guess == randnum:
        print("Nice guess " + a + "! Bye, have a nice day!")

Edit: Also, you dont want to use is every time: Is there a difference between "==" and "is"?

Answered By – Tzane

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published