Disclaimer
Some people want to use my book Python for Kids for Dummies to learn Python 3. I am working through the code in the existing book, highlighting changes from Python 2 to Python 3 and providing code that will work in Python 3.
If you are using Python 2.7 you can ignore this post. This post is only for people who want to take the code in my book Python for Kids for Dummies and run it in Python 3.
Using Python3 in Project 5 of Python For Kids For Dummies
Project 5 introduces the concept of using functions for performing repetitive work when you’re programming. All of the keywords introduced in this Project are the same and have the same syntax in Python 3, so you really shouldn’t have any trouble with this code – except for the fact that some of the code has raw_input in it and that caused problems in earlier chapters. As I mentioned in my earlier post you can either:
- replace every reference to raw_input by plain old input; or
- at the top of your file add the line raw_input = input.
If you’re cutting and pasting from the code on the website this is probably the easiest thing to do.
Basically, everything in this project will work if you use one of the two strategies above. However, some of the code will give a different output when run under Python3 compared to Python2.7. There is also one place where the code in the book is redundant. This is the use of the float() builtin – Python3 automatically calculates its results using decimals.
Page 105 – 110:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7
Page 111-112
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working.
#Python 2.7 code: import random computers_number = random.randint(1,100) prompt = 'What is your guess? ' while True: players_guess = raw_input(prompt) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high')
#Python 3 code: import random raw_input = input # this fixes raw_input! computers_number = random.randint(1,100) prompt = 'What is your guess? ' while True: players_guess = raw_input(prompt) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high')
Page 113
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working.
#Python 2.7 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random computers_number = random.randint(1,100) PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" while True: players_guess = raw_input(PROMPT) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high') do_guess_round()
#Python 3 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random raw_input = input # this fixes raw_input! computers_number = random.randint(1,100) PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" while True: players_guess = raw_input(PROMPT) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high') do_guess_round()
Pages 114 – 116:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7
Page 117
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working.
#Python 2.7 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random computers_number = random.randint(1,100) PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" computers_number = random.randint(1,100) # Added while True: players_guess = raw_input(PROMPT) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high') while True: # Print statements added: print("Starting a new Round!") print("The computer's number should be "+str(computers_number)) print("Let the guessing begin!!!") do_guess_round() print("") # blank line
#Python 3 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random raw_input = input # this fixes raw_input! computers_number = random.randint(1,100) PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" computers_number = random.randint(1,100) # Added while True: players_guess = raw_input(PROMPT) if computers_number == int(players_guess): print('Correct!') break elif computers_number > int(players_guess): print('Too low') else: print('Too high') while True: # Print statements added: print("Starting a new Round!") print("The computer's number should be "+str(computers_number)) print("Let the guessing begin!!!") do_guess_round() print("") # blank line
Pages 118 – 129:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7
Pages 130-131:
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working.
#Python 2.7 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" computers_number = random.randint(1, 100) number_of_guesses = 0 # Added while True: players_guess = raw_input(PROMPT) number_of_guesses = number_of_guesses+1 # Added if computers_number == int(players_guess): print('Correct!') return number_of_guesses # Changed elif computers_number > int(players_guess): print('Too low') else: print('Too high') total_rounds = 0 # Added total_guesses = 0 # Added while True: total_rounds = total_rounds+1 # Added print("Starting round number: "+str(total_rounds)) # Changed print("Let the guessing begin!!!") this_round = do_guess_round() # Changed total_guesses = total_guesses+this_round # Added print("You took "+str(this_round)+" guesses") # Added avg = str(total_guesses/float(total_rounds)) # Added print("Your guessing average = "+avg) # Added print("") # blank line
#Python 3 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random raw_input = input # this fixes raw_input! PROMPT = 'What is your guess? ' def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" computers_number = random.randint(1, 100) number_of_guesses = 0 # Added while True: players_guess = raw_input(PROMPT) number_of_guesses = number_of_guesses+1 # Added if computers_number == int(players_guess): print('Correct!') return number_of_guesses # Changed elif computers_number > int(players_guess): print('Too low') else: print('Too high') total_rounds = 0 # Added total_guesses = 0 # Added while True: total_rounds = total_rounds+1 # Added print("Starting round number: "+str(total_rounds)) # Changed print("Let the guessing begin!!!") this_round = do_guess_round() # Changed total_guesses = total_guesses+this_round # Added print("You took "+str(this_round)+" guesses") # Added avg = str(total_guesses/float(total_rounds)) # Added print("Your guessing average = "+avg) # Added print("") # blank line
Page 132:
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working. The code here is only a single side function. If you have the raw_input = input line earlier in the file you don’t need to add it again. I’m showing it here just for completeness.
#Python 2.7 code: CONFIRM_QUIT_MESSAGE = 'Are you sure you want to quit (Y/n)? ' def confirm_quit(): """Ask user to confirm that they want to quit default to yes Return True (yes, quit) or False (no, don't quit) """ spam = raw_input(CONFIRM_QUIT_MESSAGE) if spam == 'n': return False else: return True
#Python 3 code: CONFIRM_QUIT_MESSAGE = 'Are you sure you want to quit (Y/n)? ' def confirm_quit(): """Ask user to confirm that they want to quit default to yes Return True (yes, quit) or False (no, don't quit) """ spam = input(CONFIRM_QUIT_MESSAGE) if spam == 'n': return False else: return True
Pages 133-134:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7
Page 135:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7. However:
- see the errata on Project 5. The line avg = str(total_guesses/float(total_rounds)) needs to be moved; and
- the use of float() is unnecessary. We did this because Python 2.7 thinks if you divide a whole number by a whole number you want a whole number as an answer. If you want a decimal you need to use float(). Python3 calculates using decimals by default so float() is unnecessary.
Here is the corrected code (works in both Python 2.7 and Python 3):
# new if condition (and code block) to test against quit if this_round == QUIT: total_rounds = total_rounds - 1 # removed line from here if total_rounds == 0: stats_message = 'You completed no rounds. '+\ 'Please try again later.' else: avg = str(total_guesses/float(total_rounds)) # to here stats_message = 'You played ' + str(total_rounds) +\ ' rounds, with an average of '+\ str(avg) break
Page 136:
All code on these pages is the same, and all outputs from the code is the same in Python 3 as in Python 2.7.
Page 137:
This code has raw_input. Add a line raw_input = input # this fixes raw_input! at the top of the file to get it working. You also need to incorporate the fixes mentioned in relation to the code on page 135.
Here is the corrected code in Python 3:
#Working Python 3 code: """guess_game_fun Guess Game with a Function In this project the guess game is recast using a function""" import random raw_input = input # this fixes raw_input! PROMPT = 'What is your guess? ' # New constants QUIT = -1 QUIT_TEXT = 'q' QUIT_MESSAGE = 'Thank you for playing' CONFIRM_QUIT_MESSAGE = 'Are you sure you want to quit (Y/n)? ' # New confirm_quit function def confirm_quit(): """Ask user to confirm that they want to quit default to yes Return True (yes, quit) or False (no, don't quit) """ spam = raw_input(CONFIRM_QUIT_MESSAGE) if spam == 'n': return False else: return True def do_guess_round(): """Choose a random number, ask the user for a guess check whether the guess is true and repeat until the user is correct""" computers_number = random.randint(1, 100) number_of_guesses = 0 while True: players_guess = raw_input(PROMPT) # new if clause to test against quit if players_guess == QUIT_TEXT: if confirm_quit(): return QUIT else: continue # that is, do next round of loop number_of_guesses = number_of_guesses+1 if computers_number == int(players_guess): print('Correct!') return number_of_guesses elif computers_number > int(players_guess): print('Too low') else: print('Too high') total_rounds = 0 total_guesses = 0 while True: total_rounds = total_rounds+1 print("Starting round number: "+str(total_rounds)) print("Let the guessing begin!!!") this_round = do_guess_round() # new if condition (and code block) to test against quit if this_round == QUIT: total_rounds = total_rounds - 1 if total_rounds == 0: stats_message = 'You completed no rounds. '+\ 'Please try again later.' else: avg = str(total_guesses/float(total_rounds)) stats_message = 'You played ' + str(total_rounds) +\ ' rounds, with an average of '+\ str(avg) break total_guesses = total_guesses+this_round avg = str(total_guesses/float(total_rounds)) print("You took "+str(this_round)+" guesses") print("Your guessing average = "+str(avg)) print("") # Added exit messages print(stats_message)