[Tutor] Wrapping my head around global variables!!
Dave Angel
davea at ieee.org
Fri Jan 28 21:01:24 CET 2011
On 01/-10/-28163 02:59 PM, Nevins Duret wrote:
> Hello Python collective,
>
> I am trying to wrap my head around what exactly is causing me not to get
> any output or error message in the following code:
>
>> #!/usr/bin/env python3.1
>>
>> import random
>>
>> def main():
>>
>> def chosen_letter():
>>
>> chosen_letter = Consonant()
>> chosen_letter = Vowel()
>>
>> return chosen_letter
>>
>> Consonant = random.choice( [B, C, D, F, G, H, J, K, L, M, N, P, Q, R,
>> S, T, V, W, X, Z] )
>> Vowel = random.choice( [A, E, I, O, U, Y] )
>> print("Choose a letter"( Consonant, Vowel ))
>> print("You randomly chose"( Consonant, Vowel ))
>>
>> if Consonant == ( "B C D F G H J K L M N P Q R S T V W X Z" ):
>> print("You randomly chose a Consonant")
>> else:
>> print("You randomly chose a Vowel")
>>
>> if Consonant:
>> gameStatus = "CONTINUE"
>> elif Vowel == A or Vowel == E or Vowel == I or Vowel == O or Vowel ==
>> U or Vowel == Y:
>> gameStatus = "FINISHED ALL VOWELS"
>>
>> if gameStatus == "FINISHED ALL VOWELS":
>> print("FINISHED DISPLAYING ALL VOWELS")
>> else:
>> print("LOOKING FOR MORE VOWELS")
>>
>> if __name__ == "__main__": main()
>>
> All this code is intended to do is to have a user, in this case my 4
> year old, choose letters of the alphabet until all vowels are specified.
> For whatever reason I'm neither getting an error message nor am I
> obtaining any messages in the output console. Any help on this would be
> greatly appreciated. I eventually want to make this into a gui in order
> to introduce young kids to programming, by having the letters print out
> to screen as the user makes choices based on vowels and consonants.
> Again, thank you for any help and expertise.
>
> Best Regards,
>
> freesparks
>
Not too clear what you're up to here. The first problem is that your
main() function returned before it did anything useful. That's because
you didn't indent the return statement of the nested chosen_letter()
function to match the rest of the body. Since it was indented like the
body of main(), the rest of main() was ignored.
Having said that, you have a pile of problems in this code. Not a good
idea to name local variables the same as the function name. This isn't
BASIC. And you assign two different values to the same name. And you
call two functions, Consonant() and Vowel(), when the only other symbols
by those names are probably character strings.
You call random.choice with a list made up of undefined symbols. You'll
need to put quotes around each of "B", "C", etc.
You compare Consonant to a string, when you probably meant to check if
it was in the string. Consider using the "in" operator
And it looks like you meant to have a loop inside main(), since your
last print is "LOOKING FOR MORE VOWELS"
HTH,
DaveA
More information about the Tutor
mailing list