This topic is READ ONLY
DandDad (4) [Avatar] Offline
The exercise errors out as written because the input() function return a string that can't be compared with the secret which is an int.

The modification below fixes the problem:
guess = int(input("What's yer guess? ")) # get the player's guess

Is that problem specific to Python version 3? Is there a more elegant solution?
warren.sande (211) [Avatar] Offline
Re: Listing 1-2 in Python v3
Yes, that problem is specific to Python 3 - it does not happen with Python 2.x. This is one of the more noticeable changes with Python 3.

In Python 2.x, we have input(), which returns a number ( * see further explanation below), and raw_input() which returns a string.

In Python 3, there is no more raw_input(), but Python 3's input() works like Python 2.x raw_input(); that is, it returns a string.

There is no comparable function in Python 3 to the Python 2 input() function - nothing that ask for input from the user and directly returns a number.

Your modification below is the best solution for the Number Guess program, where you are expecting an integer.

Warren and Carter

* further explanation

Actually, input() returns an evaluation of whatever is typed in. If a number is typed in, it returns the number. If a string in quotes is typed in, it returns the string. If a variable name is typed in, it returns the value of the variable. If a Python statement is typed in, it returns whatever that statement would return.

This actually makes input() rather "unsecure". For example, in the NumberGuess game, you can cheat by typing in "secret" when it asks for your guess. Since secret is the number you're trying to guess, it will tell you that you've won!

The eval() function (which is in both Python 2.x and Python 3.x) does the same thing, except it doesn't get the input from the console, it evaluates whatever is passed to it.
DandDad (4) [Avatar] Offline
Re: Listing 1-2 in Python v3
One more thing if I may.
I'm trying to build an error handler in the game when the player enters a non-number guess. I added a try-except loop but I can't tell how to make the program go back to asking for a new guess and skip the entire "answer" loop when the exception is raised. It would replace the "tries = tries - 1 " line below. Could you help me?

# Number Guess game

import random

secret = random.randint(1, 50) # pick a secret number
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 50. I'll give you 6 tries. ")

# try until they guess it or run out of turns
while guess != secret and tries < 6:
guess = int(input("What's yer guess? ")) # get the player's guess
except ValueError:
print ("Please use numbers only")
tries = tries - 1 #THIS IS WHERE I'D LIKE TO STOP AND GO BACK UP TO "try"
answer = eval('guess-secret')
if answer < 0:
print ("Too low, ye scurvy dog!")
elif answer > 0:
print ("Too high, landlubber!")
print ("Tries left", 5 - tries)
tries = tries + 1 # used up one try

# print message at end of game
if answer == 0:
print ("Avast! Ye got it! Found my secret, ye did!")
print ("No more guesses! Better luck next time, matey!")
print ("The secret number was", secret)
warren.sande (211) [Avatar] Offline
Re: Listing 1-2 in Python v3
We can help with that.

But, unfortunately, this forum strips off whitespace, so we can't see what your indenting looks like.

Please send the code you have to and we'll be glad to help.

Warren and Carter