By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,386 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

Strange Behaviour with an IF Statement

WhiteRider
P: 37
Expand|Select|Wrap|Line Numbers
  1. def load(): # function to load game data
  2.     if os.path.exists('./data.dat') == True: 
  3.         load = open('data.dat', 'r') 
  4.         save = load.readline() 
  5.         if save == "":
  6.             welcome()
  7.         elif save == "1\n" or "1": 
  8.             chce = raw_input("A saved game has been detected. Would you like to load this? Y/N ")
  9.             if chce == "Y" or "y":
  10.                 pass # Not came that far yet
  11.             elif chce == "N" or "n":
  12.                 welcome()
  13.             else:
  14.                 print "Invalid choice please choose Y for yes or N for no."
  15.     else:
  16.         print "Fatal Error: 'data.dat' not detected."
  17.  
This is an excerpt of a text-based RPG I am currently writing. The problem is when I come to line 8 where it asks if I want to load a saved game or not. For some reason I just can't understand it doesn't do what its supposed to do. One: it doesn't give me the "else" section if I type in something other than Y, y, N, n. Two: it doesn't go to my "welcome()" function if I select N or n. I get no errors. The program will just exit when I try one of those things :S

Am I missing something obvious? Perhaps a syntax issue - like a block of code not in the right place. I am not that new to Python and have made a text-based RPG before in this language. However its been a while since I started using it again so maybe I am having a noob moment. Any help would be greatly appreciated, thanks,
Sep 27 '07 #1
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Expand|Select|Wrap|Line Numbers
  1. def load(): # function to load game data
  2.     if os.path.exists('./data.dat') == True: 
  3.         load = open('data.dat', 'r') 
  4.         save = load.readline() 
  5.         if save == "":
  6.             welcome()
  7.         elif save == "1\n" or "1": 
  8.             chce = raw_input("A saved game has been detected. Would you like to load this? Y/N ")
  9.             if chce == "Y" or "y":
  10.                 pass # Not came that far yet
  11.             elif chce == "N" or "n":
  12.                 welcome()
  13.             else:
  14.                 print "Invalid choice please choose Y for yes or N for no."
  15.     else:
  16.         print "Fatal Error: 'data.dat' not detected."
  17.  
This is an excerpt of a text-based RPG I am currently writing. The problem is when I come to line 8 where it asks if I want to load a saved game or not. For some reason I just can't understand it doesn't do what its supposed to do. One: it doesn't give me the "else" section if I type in something other than Y, y, N, n. Two: it doesn't go to my "welcome()" function if I select N or n. I get no errors. The program will just exit when I try one of those things :S

Am I missing something obvious? Perhaps a syntax issue - like a block of code not in the right place. I am not that new to Python and have made a text-based RPG before in this language. However its been a while since I started using it again so maybe I am having a noob moment. Any help would be greatly appreciated, thanks,
Your problem is here:
Expand|Select|Wrap|Line Numbers
  1. if chce == "Y" or "y"
It should be:
Expand|Select|Wrap|Line Numbers
  1. if chce == "Y" or chce == "y"
This looks better:
Expand|Select|Wrap|Line Numbers
  1. if chce.lower() == 'y':
>>> s = 'N'
>>> s == 'y' or 'Y'
'Y'
>>> if s == 'y' or 'Y':
... print "'Y' evaluates True"
...
'Y' evaluates True
>>>
Sep 28 '07 #2

WhiteRider
P: 37
Your problem is here:
Expand|Select|Wrap|Line Numbers
  1. if chce == "Y" or "y"
It should be:
Expand|Select|Wrap|Line Numbers
  1. if chce == "Y" or chce == "y"
This looks better:
Expand|Select|Wrap|Line Numbers
  1. if chce.lower() == 'y':
>>> s = 'N'
>>> s == 'y' or 'Y'
'Y'
>>> if s == 'y' or 'Y':
... print "'Y' evaluates True"
...
'Y' evaluates True
>>>
Silly me, it really was a noob moment. I mean really, what language in earth is smart enough to take 'or' as direct as that. OK back to work!

Thank you for your help bvdet :)
Sep 28 '07 #3

bartonc
Expert 5K+
P: 6,596
Silly me, it really was a noob moment. I mean really, what language in earth is smart enough to take 'or' as direct as that. OK back to work!

Thank you for your help bvdet :)
I often use the in operator in order to shorten many combined ors:
Expand|Select|Wrap|Line Numbers
  1. if chce in "Yy":
  2.     print 'yep'
Sep 28 '07 #4

WhiteRider
P: 37
Thats great. My code looks a lot neater now. Thanks for sharing that bartonc.
Sep 28 '07 #5

bartonc
Expert 5K+
P: 6,596
Thats great. My code looks a lot neater now. Thanks for sharing that bartonc.
Any time, really. And you may call me "Barton".
Thanks for joining us here on the Python Forum at TSDN!
Sep 28 '07 #6

WhiteRider
P: 37
Any time, really. And you may call me "Barton".
Thanks for joining us here on the Python Forum at TSDN!
Thank you for welcoming me Barton! This is a great forum =)
Oct 1 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.