Re: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
On 22/04/14 01:18, Alan Gauld wrote: input takes as an argument a prompt string and returns the value input by the user so your usage should look like: smv_grandVariable("Enter a 1 to play or 0 to exit:") Whoops, something went badly wrong in an edit there. It should read: smv_grandVariable = input("Enter a 1 to play or 0 to exit:") apologies for that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
On 21/04/14 19:12, Stephen Mik wrote: ...I am inputting or trying to input,a Sentry Variable to a While Loop. I want to test out the Main program" While" Loop before I add an inner "While" Loop. The program I have written,when run on the Python 3.4.0 Shell,does not stop for input of the "While" Sentry Variable,it just gives a program error: "Value of smv_grandVariable undefined". What am I doing wrong here? > import random > ... > print("Do you want to play the game?\n") > print("Enter a 1 to play or 0 to exit:") > input(smv_grandVariable) You have completely misunderstood input... input takes as an argument a prompt string and returns the value input by the user so your usage should look like: smv_grandVariable("Enter a 1 to play or 0 to exit:") But that's a terrible name for a variable. You should name variables after their purpose. What does this variable represent? You say its a sentry? So call it sentry... Having the word "variable" in a variable name is nearly always a mistake. > while (smv_grandVariable == 1 and smv_grandVariable != 0): And your second mistake is that you have not converted the string typed by the user to a number(specifically an int) but you are comparing the variable to the numbers 0,1 Finally the logic of your test can be replaced by the simpler while int(smv_grandVariable) != 0: since 1 is also not zero. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
Hi, On 21 April 2014 19:12, Stephen Mik wrote: > Dear Python Community: > I am new to Python,with only about a month's experience. I am writing > Python 3.4.0 code that apparently isn't doing what it should be doing. > Specifically, I am inputting or trying to input,a Sentry Variable to a While > Loop. I want to test out the Main program" While" Loop before I add an inner > "While" Loop. The program I have written,when run on the Python 3.4.0 > Shell,does not stop for input of the "While" Sentry Variable,it just gives a > program error: "Value of smv_grandVariable undefined". What am I doing wrong > here? You are misunderstanding how input() works. It is a function, which means it returns a result, and takes one parameter which is a prompt/message to display, e.g you should have something like this: result = input('Input a string:') This displays 'Input a string:' to the user and then waits for input, after which it puts the inputted value into the variable 'result'. That also hopefully explains the error message -- it's telling you that 'smv_grandVariable', which you've given to input() and which Python's duly trying to display is undefined. (By the way, try to pick a better name for that variable which suggests what its role is supposed to be.) Also see here: https://docs.python.org/3.4/library/functions.html#input Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
Dear Python Community: I am new to Python,with only about a month's experience. I am writing Python 3.4.0 code that apparently isn't doing what it should be doing. Specifically, I am inputting or trying to input,a Sentry Variable to a While Loop. I want to test out the Main program" While" Loop before I add an inner "While" Loop. The program I have written,when run on the Python 3.4.0 Shell,does not stop for input of the "While" Sentry Variable,it just gives a program error: "Value of smv_grandVariable undefined". What am I doing wrong here? I'll try to post that part of the Code that is malfunctioning as well as Python Shell 3.4.0 Traceback Analysis. Please help if you can,this program is due on Thursday the 22nd of April,2014.#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("Enter a 1 to play or 0 to exit:") input(smv_grandVariable) while (smv_grandVariable == 1 and smv_grandVariable != 0): #Enter the play games main loop area #Take Control of the main While Loop print("Do you want to run the Guess My Number game again? \n") print("IF so, 1 to play again or 0 to not play \n") input(smv_grandVariable) #end of main control Loop #print out ending comments print("Program is Ended") input("\n\nPress the enter key to exit. ") #End of Program 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: Traceback (most recent call last): File "E:\My Code\assignment4.py", line 37, in input(smv_grandVariable) NameError: name 'smv_grandVariable' is not defined >>> ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor