472,958 Members | 2,089 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 1470
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

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

Similar topics

3
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ...
10
by: tborn | last post by:
Hi there, Not sure if any one has experienced this before and can tell me what's wrong with this statement: if verified = false then dataObjects.HasError = true This is all on one line...
5
by: Praveen_db2 | last post by:
Hi All db2 8.1.3 Windows I have folowing table structures CREATE TABLE tb_RTB( EMP_ID INTEGER, DESC VARCHAR(20)); CREATE TABLE tb_ERROR( SQL_STATE CHAR(5), SQL_DESC VARCHAR(20),
8
by: Dox33 | last post by:
I ran into a very strange behaviour of raw_input(). I hope somebody can tell me how to fix this. (Or is this a problem in the python source?) I will explain the problem by using 3 examples....
8
by: ananthaisin | last post by:
How to reduce the table size for any table while using truncate or delete statements. In oracle 8i it was truncating the storage space but in 10g it is not .... I have given truncate statement in...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.