trying to make a simple program help please :)

BartC bc at freeuk.com
Fri Oct 14 16:43:34 EDT 2016


On 14/10/2016 19:53, LongHairLuke wrote:
> Den fredag 14 oktober 2016 kl. 20:30:20 UTC+2 skrev MRAB:
>> On 2016-10-14 19:11, LongHairLuke wrote:
>>> Hi, l l am trying to make a simple guess program. This is my script:
>>>
>>> def main():
>>>     print ("Guess a letter between a and e")
>>>     randomNumber = b
>>>
>>>     userGuess = input("Your guess: ")
>>>
>>> if userGuess == randomNumber:
>>>      print("You got it")
>>> else:
>>>      print ("That's not it")
>>>
>>> main()
>>>
>>>
>>> When l run the program l get error: userGuess is not defined, but l defined userGuess at line 5. I don't get it someone plese help :)
>>>
>> Lines 7 to 10 should be indented like the preceding lines. The way
>> you've written them, they're outside 'main'.
>
> indented? can you explain :)
>

def main():
     print ("Guess a letter between a and e")
     randomNumber = b

     userGuess = input("Your guess: ")

     if userGuess == randomNumber:
         print("You got it")
     else:
         print ("That's not it")

main()

(Python doesn't have explicit block-end delimiters. The end of a block 
(of the one compromising the body of function main in this case) has to 
be inferred from encountering some statement at the same or lower indent 
level than the opening 'def'. Or the end of the file.)

Anyway, the above won't completely fix the program as you haven't 
defined b, although you may have intended "b", even though you've called 
the variable 'randomNUMBER'.

With "b" in place of b, the program works on Python 3, although it 
doesn't verify bad inputs.

-- 
Bartc



More information about the Python-list mailing list