Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this Python game code?

Tags:

python

import random

secret = random.randint (1,99)
guess = 0
tries = 0

print ("AHOY! I'm the Dread Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries. ")

while guess != secret and tries < 6:
    guess = input ("What's yer guess? ")
    if guess < secret:
        print ("Too low, ye scurvy dog")
    elif guess > secret:
        print ("Too high, landrubber!")
    tries = tries + 1
if guess == secret:
    print ("Avast! Ye got it! Found my secret, ye did!")
else:
    print ("No more guesses! Better luck next time, matey!")
    print ("The secret number was", secret)

I keep getting this error: if guess < secret: TypeError: unorderable types: str() < int()

like image 218
Royce Avatar asked Dec 02 '22 05:12

Royce


1 Answers

guess = input ("What's yer guess? ")

Calling input gives you back a string, not an int. When you then compare guess using <, you need an int in order to compare a numerical value. Try doing something along the lines of:

try:
    guess = int(input("What's yer guess? "))
except ValueError:
    # Handle bad input
like image 150
Justin Ardini Avatar answered Dec 05 '22 01:12

Justin Ardini