The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

This topic is READ ONLY
cindy (1) [Avatar] Offline
#1
We typed in the prompts and get an error on Line 7. The problem seems to be with secret and tries. The and keeps coming up red. Any ideas? Thank you. BTW-we are total newbies at this programming stuff.
Ben (1) [Avatar] Offline
#2
Hi Cindy,
The reason for your problem might be that you are using later Python version other than Python 2.5 as it did in this book. I'm using Python 3.5 for the examples in this book, and encountered the same problems as you mentioned.

In Python 3, input() always returns a string value. Use the int() type to convert it:

if int (guess) < int (secret):




Here is the revised code for example 1.2 in Python 3.5

Hope this helps.

# Number Guess game

import random

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

# try until they guess it or run out of turns
while guess != int (secret) and int (tries) < 6:
guess = input("What's yer guess? ") # get the player's guess
if int (guess) < int (secret):
print ("Too low, ye scurvy dog!")
elif int (guess) > int (secret):
print ("Too high, landlubber!")
tries =tries + 1 # used up one try

# print message at end of game
if int (guess) == int (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"), print (secret)