-1

I am a novice coder who's just started coding about 4-5 weeks ago. The best I have achieved is a basic Python username and password login page for a 'top secret' website (the website is fake). However, just to refresh my memory of basic coding (I've been doing some un-related things lately), I tried making a basic childrens game to do with the alphabet. Here's my current code:

name = input("What's Your Name?:  ")
print("Welcome" , name , "to the 3 lucky guess alphabet skills builder!")
print("Let's get started:")
C = input("What Is The 3rd Letter of The Alphabet:  ")
if C == 'C' or 'c':
    print("Congraulations!")
else:
    print("Think We Should Retry That!")
    C
    if C == 'C' or 'c':
          print("That's Better!")
Z = input("What Is The Last Letter of The Alphabet:  ")
if Z == 'Z' or 'z':
    print("You're Really Good At This! One More!")
else:
    print("Have Another Go!")
    Z
    if Z == 'Z' or 'z':
        print("That's More Like It! Last One!")
J = input("What Is The 10th Letter Of The Alphabet:  ")
if J == 'J' or 'j':
    print("Great! How Many Did You Get In Total?")
else:
    print("Unlucky, Better Luck Next Time!")

total = input("How Many Did You Get In Total?:  " , print("Out Of 3!")
print("Wow! You Got" , total , "! Well Done" , name , "!!!")
exit

Why aren't any of the 'else' arguments working?

Also, why won't the second to last bit of code work - it just states a syntax error!

I have tried indenting all the else statements but that results in a syntax error too!

Please help! :)

2 Answers2

2

The if statement you wrote, like the following

if C == 'C' or 'c':

doesn't do what you mean. The expression after or just checks whether 'c' evaluates to true, which it always will. That's why the code after else: won't execute.

You have to write it like this:

if C == 'C' or C == 'c':
  • This is probably the error that you're seeing right now, but when you get past this one, you'll reveal the error in my answer. Since you can't accept both, I suggest you accept this one, since it's closer to the question you were trying to ask. :) – Jon Kiparsky Oct 11 '14 at 20:29
0

It's difficult to know what you mean by "not working" - you should be more specific about the error you're seeing. Are you referring to this?

else:
    print("Have Another Go!")
    Z
    if Z == 'Z' or 'z':
        print("That's More Like It! Last One!")

The second line of the body simply evaluates the variable Z - it doesn't change anything. Therefore, the conditional following it still returns the same result as the last time you evaluated it.

Also, as the other answer points out,

if a = "foo" or "bar"

will always be True, because "bar" is a non-false value, and an or with any non-false value is True.

Jon Kiparsky
  • 7,499
  • 2
  • 23
  • 38