Talk:Non-Programmer's Tutorial for Python 2.6/Decisions

Shouldn't this part

number = 42 guess = 0 count = 0 while guess != number: count = count + 1 guess = input('Guess a number: ') if guess > number: print 'Too high' elif guess < number: print 'Too low' if count > 3: print 'That must have been complicated.' else: print 'Just right'

be something like this:

number = 72 guess = 0 wrong = 0 while guess != number: guess = input ("Guess a number: ") if guess != number: wrong = wrong + 1 if guess > number: print "Too high" elif guess < number: print "Too low" print "Just right" if wrong > 3: print "That must have been complicated."
 * 1) Plays the guessing game higher or lower
 * 1) This should actually be something that is semi random like the
 * 2) last digits of the time or something else, but that will have to
 * 3) wait till a later chapter.  (Extra Credit, modify it to be random
 * 4) after the Modules chapter)

because i think in the first version you actually have to enter 2 wrong numbers to get that message
 * Try it and see :) Since it is using >, it actually needs to be a count of 4 to get the "That must be complicated" message, which means that their are 3 wrong guesses and one right guess.  Jrincayc (talk) 14:57, 15 February 2008 (UTC)

Use of input is heavily discouraged in python 2.x
the input command is really, really dangerous -- always use raw_input and convert to what you actually want (floats or integers for numbers, obviously). You can wreck some real havoc with the input command; consider that input in python 2.x acts like eval(raw_input) and you should realize how dangerous it is:

>>> import subprocess

>>> input("input: ")

input: subprocess.Popen(('ping', 'www.google.com')) 

>>> PING www.l.google.com (66.102.11.104) 56(84) bytes of data.

64 bytes from syd01s01-in-f104.1e100.net (66.102.11.104): icmp_req=1 ttl=57 time=18.5 ms

That's just a single example. input lends itself to injection attacks in python 2.x (it works like raw_input in python 3.x though!). As such, I feel that the example should be changed to use raw_input instead.


 * I agree. Go for it.  I fixed this in the tutorial for python 3.  You can use that as a basis, or modify it in some other way.  Jrincayc (discuss • contribs) 17:29, 23 January 2011 (UTC)