943,602 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1211
  • Python RSS
Mar 25th, 2008
0

Newbie Help

Expand Post »
Howdy, hoping I can get alittle help today.

First off I have a real bad habit of learning one thing, and getting very fascinated by it and going off on a tangent using what i learned in as many ways as I can think of.

Im a very new person to python, and programing in general. I started doing the tutorials and got to the If/Elif/Else chapter and thought of all the cool things I could do with that.

I started writing a story, that used if/else statements to steer the story in the direction the reader chose, Modeled after the old choose your own adventures and an idea in the beginners project ideas thread.

the code looks alittle like this.

Python Syntax (Toggle Plain Text)
  1. print "Please choose your hero"
  2. sex = raw_input("(M)ale or (F)emale: ")
  3. if sex = "M":
  4. print "A young and adventerous lad out to save the world!"
  5. else:
  6. print "Look out Lara Croft, there is a new show in town"
  7.  
  8. print "_"*100
  9. print " "
  10. print "Now for your body build"
  11. body = raw_input("Are you (S)lim, (A)verage, (M)uscular, or (H)eavy")
  12.  
  13. if body = "S":
  14. print "Thin and wiry, your a willow in the wind"

didnt print the rest, because its pretty much the same throughout the script.

As i was testing it, i noticed that its cap sensitive. If they press "m" for male, it doesnt register, just hangs up the program.
What Im looking for help with, is how do i have it check for "M", "m" instead of just M

Ive tried various ways just buy guessing

Python Syntax (Toggle Plain Text)
  1. if sex = "M", "m"
  2. ["M", "m"]
  3. {"M", "m"}
  4. ["M":"m"]

And i dont seem to be able to figure it out.
Any help, be it examples, or links to tutorials, or just general guidelines, it would all be greatly appreciated.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwarguy is offline Offline
15 posts
since Mar 2008
Mar 25th, 2008
0

Re: Newbie Help

edit limit expired. the = in the if statements are supposed to be ==
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwarguy is offline Offline
15 posts
since Mar 2008
Mar 25th, 2008
0

Re: Newbie Help

You can do:
Python Syntax (Toggle Plain Text)
  1. if sex="M" or "m"

or and and
Reputation Points: 118
Solved Threads: 30
Posting Shark
linux is offline Offline
931 posts
since Aug 2006
Mar 25th, 2008
0

Re: Newbie Help

For a given string s, s.lower() will return the same string entirely in lower case. So, rather than

python Syntax (Toggle Plain Text)
  1. if sex == "M":

you could use

python Syntax (Toggle Plain Text)
  1. if sex.lower() == "m":
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Mike W is offline Offline
4 posts
since Mar 2008
Mar 26th, 2008
0

Re: Newbie Help

Thanks for the help guys, those worked wonderfully
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwarguy is offline Offline
15 posts
since Mar 2008
Mar 26th, 2008
0

Re: Newbie Help

Actualy that didnt work. I changed the statements to

if sex == "M" or "m":

and i didnt get any error codes when i compiled and ran threw the first test and it worked

I later went back and picked different options, and it always gave me the first option, irregardless of what option i selected.
This is my code so far.

Python Syntax (Toggle Plain Text)
  1. print "Please choose your hero"
  2. sex = raw_input("(M)ale or (F)emale: ")
  3. if sex = "M" or "m":
  4. print "A young and adventerous lad out to save the world!"
  5. else:
  6. print "Look out Lara Croft, there is a new show in town"
  7.  
  8. print "_"*100
  9. print " "
  10. print "Now for your body build"
  11. body = raw_input("Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: ")
  12.  
  13. if body == "S" or "s":
  14. print "Thin and wiry, your a willow in the wind"
  15. elif body == "A" or "a":
  16. print "You could pass by him and never notice him, or his gun."
  17. elif body == "M" or "m":
  18. print "Tall and hulking, you wouldnt want to run into him in a dark alley"
  19. else:
  20. print "Hey, big people need love too!"

When i run threw the first test run i just picked the first option because its fastest

here is the output
Please choose your hero
(M)ale or (F)emale: M
A young and adventerous lad out to save the world!
____________________________________________________________________________________________________

Now for your body build
Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: S
Thin and wiry, your a willow in the wind


Now when I run the script, and choose anything but Capitalized First option, this is what i get

Please choose your hero
(M)ale or (F)emale: F
A young and adventerous lad out to save the world!
____________________________________________________________________________________________________

Now for your body build
Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: M
Thin and wiry, your a willow in the wind



So adding the "or" to the if/else statement makes it default to always choosing the first option in each code block, irregardless of which option you pick, upper or lower case.
Last edited by gwarguy; Mar 26th, 2008 at 11:36 am. Reason: text blocks look horrible when printing output
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwarguy is offline Offline
15 posts
since Mar 2008
Mar 26th, 2008
0

Re: Newbie Help

Quote ...
Actualy that didnt work. I changed the statements to
if sex == "M" or "m":
An if() statement with an "or" is the same as two if statements (or an elif), so the above equates to
if sex == "M":
elif "m":
I think you want --> if sex=="m"
"if m" is always true, since it is not==zero/False, so this always executes. BTW an if() statesment with an "and" equates to nested if statements. You might want to take another look at the basic design. Python has containers for a reason. You can use a list for the question, and a dictionary for the answer. Next, try adding a third option to the code below to help understand how it works.
Python Syntax (Toggle Plain Text)
  1. literal_list = [ ["Please choose your hero", "(M)ale or (F)emale: "],
  2. ["Now for your body build", "Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: "]]
  3. ## dictionary key=M+0 is necessary because you also use M for Muscular
  4. ## The zero relates to the zero element of literal_list=Male
  5. ## M+1 is the "1" element of literal_list=Muscular
  6. answer_dic ={"M0":"A young and adventerous lad out to save the world!",
  7. "F0":"Look out Lara Croft, there is a new show in town",
  8. "S1":"Thin and wiry, your (you're) a willow in the wind",
  9. "A1":"You could pass by him and never notice him, or his gun.",
  10. "M1":"Tall and hulking, you wouldnt want to run into him in a dark alley",
  11. "H1":"Hey, big people need love too!"}
  12.  
  13. j=0
  14. stop=len(literal_list)
  15. while j < stop:
  16. print "j =", j ## for testing
  17. print literal_list[j][0]
  18. answer = raw_input(literal_list[j][1])
  19. key = answer.upper() + str(j)
  20. print "the key is", answer.upper(), str(j), key # to see what is going on
  21. if key in answer_dic:
  22. print answer_dic[key], "\n"
  23. j += 1
  24. else:
  25. print answer, "is not a correct response...try again"
Last edited by woooee; Mar 26th, 2008 at 1:36 pm.
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is offline Offline
2,302 posts
since Dec 2006
Mar 26th, 2008
1

Re: Newbie Help

You cannot use the line:
python Syntax (Toggle Plain Text)
  1. if sex == "M" or "m":
In Python you have to write it this way:
python Syntax (Toggle Plain Text)
  1. if sex == "M" or sex == "m":
Actually you might just use:
python Syntax (Toggle Plain Text)
  1. if sex in ["M", "m"]:
Last edited by ZZucker; Mar 26th, 2008 at 2:29 pm. Reason: code tags
Reputation Points: 309
Solved Threads: 43
Practically a Master Poster
ZZucker is offline Offline
676 posts
since Jan 2008
Mar 26th, 2008
0

Re: Newbie Help

doing it like if body == "A" or body == "a"
all throughout the story, thanks for the help !
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gwarguy is offline Offline
15 posts
since Mar 2008
Mar 26th, 2008
0

Re: Newbie Help

Click to Expand / Collapse  Quote originally posted by ZZucker ...
You cannot use the line:
python Syntax (Toggle Plain Text)
  1. if sex == "M" or "m":
In Python you have to write it this way:
python Syntax (Toggle Plain Text)
  1. if sex == "M" or sex == "m":
Actually you might just use:
python Syntax (Toggle Plain Text)
  1. if sex in ["M", "m"]:
Ah! Thanks for pointing that out. I was insanely tired from my day, decided to post. I was a bit sleepy.

Thanks again!
Reputation Points: 118
Solved Threads: 30
Posting Shark
linux is offline Offline
931 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: String problems1!
Next Thread in Python Forum Timeline: Graphing Projectile Motion with wxPython





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC