[Tutor] Beginning Python 3.4.0 Programmer:Stephen Mik: Cannot get input variable to make While Loop conditional to work
Alan Gauld
alan.gauld at btinternet.com
Tue Apr 22 02:18:19 CEST 2014
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
More information about the Tutor
mailing list