471,306 Members | 874 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,306 software developers and data experts.

Strange Behaviour with an IF Statement

WhiteRider
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
6 1297
bvdet
2,851 Expert Mod 2GB
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
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
6,596 Expert 4TB
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
Thats great. My code looks a lot neater now. Thanks for sharing that bartonc.
Sep 28 '07 #5
bartonc
6,596 Expert 4TB
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
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.

Similar topics

3 posts views Thread by Bruno van Dooren | last post: by
8 posts views Thread by Dox33 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.