-1

I am newer to Python, so please try not to yell at me, haha. I am not sure exactly what I am doing wrong here, and wanted to ask for any pointers or tips. I am trying to create a simple guessing game with Python. It states on line 10 my code has an error, and I am not sure exactly what it is. Sorry I cannot be more specific, I do not know all the basics yet, and I am attempting to figure out how to make programs. Here is my code below.

num = 30
guess = ""
guess_count = 0
guess_limit = 3
out_of_guesses = False

while num != guess:
    print()
    if guess < num:
        print("Sorry, but your guess is too low! ")
        guess = int(input("Guess the number I am thinking of: "))

    elif guess > num:
        print("Sorry, but your guess is too high! ")
        guess = int(input("Guess the number I am thinking of: "))
    else:
        print("Wow! You guessed it, good job! ")

if out_of_guesses:
    print("Out of guesses, sorry but you lost! ")
ratcrux
  • 3
  • 2
  • 1
    Your problem is that you compare `guess` with `num`, but `guess` is a string and `num` is an integer. –  Dec 25 '19 at 21:09

3 Answers3

1

Welcome!
Please don't share your code using images. There is an easy way to directly share code as text on stackoverflow. Look here for examples.

As for your question, raw_input was renamed in python 3 to input. See this question.

Jona
  • 669
  • 10
  • 18
0

if guess < num:

Strings and integers cannot be compared using comparison operators. This is because strings and integers are different data types. So in line 2 you should be change guess = 0 instead of guess = ""

0
import random

def guess(x):
    guess = 1 
    random_num = random.randint(1,x)
    while (guess != random_num) :
              
        guess = int(input(f"Enter number between 1 and {x}: "))
        
        if guess > random_num:
            print("Sorry, guess again, Too high number")
        elif guess < random_num:
            print("Sorry, guess again, Too low number")
        elif   guess == random_num:  
            print(f"Congrats,,,, you guessed the number {random_num}" )

guess(20)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 25 '22 at 19:45