0

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!")    
else:    
    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!")    
else:    
    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!")
else:
    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.")
    else:
        print("Oh that's sad. I'll work on it. I really like magic tricks and games.")
else:
    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:
        print("Higher.")
    if guess > randnum:
        print("Lower.")
Kusal Darshana
  • 159
  • 2
  • 18
MG195
  • 13
  • 2
  • Your while loop runs forever if you guess higher or lower. Only if guess == randnum it stops. Because your condition is false. You dont need a while loop at all. – Legit007 Sep 23 '21 at 06:28
  • Some tips: you can use `in` to check if a string is *any* of multiple different strings, so `if z in ("Good", "Great", "good", "great")` will behave the same way without having to write all those `or` and `==` statements. Even better, you can use `z.lower() in ("good", "great")`. The `.lower()` method returns a lowercase version of the string, so you can do case-insensitive checks. – ddejohn Sep 23 '21 at 07:36

1 Answers1

1

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!")
        break

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

Tzane
  • 2,752
  • 1
  • 10
  • 21
  • Thanks, it worked. Also, is there a way to give multiple guesses in the number guessing game part? So that they get multiple tries? Or do I just repeat parts of the code? – MG195 Sep 24 '21 at 04:30
  • Sure you can do that, check: https://www.geeksforgeeks.org/taking-multiple-inputs-from-user-in-python/ – Tzane Sep 24 '21 at 06:09