Dear Sir(s): I am new to Python programming,and I have a "Guess My Number" program which partially works. The main while control works,the guessing of an integer between 1 and 60 seems to give the "too high" or "too low" elif branches effectively. However,when the correct number is guessed the "elif" for the Congratulatory Message does not print out,and the number of attempts at guessing the mystery number does not print out. Instead, the program apparently goes into the main while control loop again and queries the User if they want to run the program again. I have attached a sample Python Shell run;along with code fragments of the relevant areas. Anybody,please help me work out this code and get "Guess My Number" correctly running. CONCERNED,Stephen W. Mik
#CS 110A Spring 2014 Sect 4988 Assignment 4 #MODIFIED GUESS MY NUMBER PROGRAM #Date April ,2014 #Programmer Stephen W. Mik #This program is modified after a version found #in the TextBook "Python Programming 3rd Edition" #by Michael Dawson Copyright 2010;ISBN-13: 978-1-4354-5500-9 #Or the Alternate ISBN-10: 1-4354-5500-2. #It is based on the material found on page 50,and pages 81-84 #of the Book, along with a downloaded basic program Structyre #"guess_my_number" ,found in the book companion's Website Files #"www.courseptr.com/downloads" Chapter 3. ######EXPLANATION OF GUESS MY NUMBER################################################### #This program uses a random number generator(supplied by Python 3.4.0) to #to pick a changeable number between 1 and 60. The User is prompted to guess the number #by inputting a guess at the prompt. The user is then advised whether the correct number #has been guessed. If it has been guessed correctly,the user is congratulated. If the number #has not been guessed correctly,the attempt is noted and quantified. then the user is told whether # the guess number was too large (and then for the user to guess a lower number) or the number #guessed was too small (and in this case for the user to guess a higher number). The number of #attempted non-successful guesses is accumulated and totalled and then when finally the User guesses #the correct number ; the guess attempts are outputted along with Congatulations. The User is queried #if they want to play the game again,if not,the Program ends. ########################################################################################## ####MAIN PROGRAM SECTION######################## #Use the random import module for a random number between 1 and 60
import random print("\tWelcome to the Guess My Number Game! ") print("\n The Mysterious Number to guess is between 1 and 60. ") print("Try to guess the Mystery Number in as few tries as you can.\n") #Enter Main Query Loop print("Do you want to play the game?\n") print("\n") smv_grandCounter=int(input("Enter a 1 to play or 0 to exit: ")) while(smv_grandCounter==1): #Enter the play game main loop area smv_pickNumber=random.randint(1,60) #Create by the computer by random number generator a mystery number #Load random number into smv_pickNumber for storage #Initialize the variable smv_attemptCounter to 1 for 1 initial pass through the inner boundary values loop smv_attemptCounter=1 #Get first pick for Number Identification Loop #Initialize smv_guessNumber so Number Identification Loop can proceed with Conditional print("Think out a first guess between 1 and 60:") smv_guessNumber= int(input("Take a guess!")) #Enter Number Identification Loop while(smv_guessNumber!=smv_pickNumber): if (smv_guessNumber > smv_pickNumber): print("Guess of mystery number Too high,enter a lesser number: \n") smv_attemptCounter+=1 elif (smv_guessNumber < smv_pickNumber): print("Guess of mystery number Too Low.Enter a greater number \n") smv_attemptCounter+=1 elif (smv_guessNumber == smv_pickNumber): #Print Congratulatory Message,the mystery number,the number of attempts print("Congratulations! You have guessed the mystery number") print("You have used",smv_attemptCounter,"sample picks.\n") print("The mystery number was: ",pick_Number) print("You have utilized",smv_attemptCounter,"attempts") else: print("You have entered a decimal number,a negative number or some other Alphanumeric character") print("Redo the loop with the correct values") #Continue the loop,increment the number of attempts smv_attemptCounter+=1 print("Enter a new integer guess:\n") smv_guessNumber=int(input("Take a new guess!")) #End the inner while Loop print("Do you want to run the game again?") #Stop the game,enter any integer number except 1 print("enter 1 to continue the game,other integer to end the game \n") smv_grandCounter=int(input("Enter 1 to continue or 0 to stop the game: ")) #print out the variable names and their corresponding labels print ("smv_grandCounter:Used in the Main Program While Loop as a Sentry Variable for program entry and exit ") print("smv_pickNumber:A main comparison variable,it receives a random number, the \'mystery number\' an integer to guess ") print("smv_guessNumber:An inputted variable,the User Guess,used to compare to smv_pickNumber,to find the \'mystery\' number.") print("smv_attemptCounter: This variable is used to give the Total number of guesses needed by the User ") input("\n\nPress the enter key to exit.")
Python 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: 1 Think out a first guess between 1 and 60: Take a guess!60 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!1 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!20 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!30 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!40 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!45 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!50 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!55 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!56 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!57 Do you want to run the game again? enter 1 to continue the game,other integer to end the game Enter 1 to continue or 0 to stop the game: 1 smv_grandCounter:Used in the Main Program While Loop as a Sentry Variable for program entry and exit smv_pickNumber:A main comparison variable,it receives a random number, the 'mystery number' an integer to guess smv_guessNumber:An inputted variable,the User Guess,used to compare to smv_pickNumber,to find the 'mystery' number. smv_attemptCounter: This variable is used to give the Total number of guesses needed by the User Think out a first guess between 1 and 60: Take a guess!55 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!30 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!40 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!39 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!38 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!37 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!36 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!35 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!34 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!33 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!32 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!31 Do you want to run the game again? enter 1 to continue the game,other integer to end the game Enter 1 to continue or 0 to stop the game: 0 smv_grandCounter:Used in the Main Program While Loop as a Sentry Variable for program entry and exit smv_pickNumber:A main comparison variable,it receives a random number, the 'mystery number' an integer to guess smv_guessNumber:An inputted variable,the User Guess,used to compare to smv_pickNumber,to find the 'mystery' number. smv_attemptCounter: This variable is used to give the Total number of guesses needed by the User Press the enter key to exit. >>> >>> ================================ RESTART ================================ >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: 1 Think out a first guess between 1 and 60: Take a guess!65 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!125 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!m Traceback (most recent call last): File "E:\My Code\assignment4.py", line 70, in <module> smv_guessNumber=int(input("Take a new guess!")) ValueError: invalid literal for int() with base 10: 'm' >>> ================================ RESTART ================================ >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: 1 Think out a first guess between 1 and 60: Take a guess!-90 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!p Traceback (most recent call last): File "E:\My Code\assignment4.py", line 70, in <module> smv_guessNumber=int(input("Take a new guess!")) ValueError: invalid literal for int() with base 10: 'p' >>> ================================ RESTART ================================ >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: 1 Think out a first guess between 1 and 60: Take a guess!987 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!30 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!20 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!21 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!22 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!23 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!24 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!25 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!26 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!27 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!28 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!29 Do you want to run the game again? enter 1 to continue the game,other integer to end the game Enter 1 to continue or 0 to stop the game: 9 smv_grandCounter:Used in the Main Program While Loop as a Sentry Variable for program entry and exit smv_pickNumber:A main comparison variable,it receives a random number, the 'mystery number' an integer to guess smv_guessNumber:An inputted variable,the User Guess,used to compare to smv_pickNumber,to find the 'mystery' number. smv_attemptCounter: This variable is used to give the Total number of guesses needed by the User Press the enter key to exit. >>> ================================ RESTART ================================ >>> Welcome to the Guess My Number Game! The Mysterious Number to guess is between 1 and 60. Try to guess the Mystery Number in as few tries as you can. Do you want to play the game? Enter a 1 to play or 0 to exit: 1 Think out a first guess between 1 and 60: Take a guess!30 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!20 Guess of mystery number Too high,enter a lesser number: Enter a new integer guess: Take a new guess!10 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!15 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!16 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!17 Guess of mystery number Too Low.Enter a greater number Enter a new integer guess: Take a new guess!18 Do you want to run the game again? enter 1 to continue the game,other integer to end the game Enter 1 to continue or 0 to stop the game:
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor