Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
why doesn't this python program work? I am running on python 3.4?
indentguessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
2 Answers
- husoskiLv 74 years ago
One problem with the pasted code is that the first few lines have an unexpected indent. (The actual indentation does show up in the delivered HTML, but Yahoo!'s style information defaults to the standard HTML procedure of eliminating "excess" spaces.) That may be a pasting artifact, but if you have mixed spaces and tabs for indents, that can cause problems.
Second, you didn't show an "import random" statement. You need import statements to load modules from the standard library and add the module name to your global names.
Everything else looks like it should work.
I don't like:
guessesTaken = str(guessesTaken)
...or the same thing with number below that. Just use str(guessesTaken) in the print() function call, or use commas to print strings and numbers separately. It's a bad habit to change the meaning of a variable (except in obvious cases of variables like "temp", with no enduring meaning.) That string conversion changes guessesTaken from a numeric counter to a display label.
- ChrisLv 74 years ago
In programming, there is no "doesn't work".
There's only the full error message you got, or a description of what happened as opposed to what was supposed to happen.