473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 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 1481
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.