Newbie Help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2008
Posts: 15
Reputation: gwarguy is an unknown quantity at this point 
Solved Threads: 0
gwarguy's Avatar
gwarguy gwarguy is offline Offline
Newbie Poster

Newbie Help

 
0
  #1
Mar 25th, 2008
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.

  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

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 15
Reputation: gwarguy is an unknown quantity at this point 
Solved Threads: 0
gwarguy's Avatar
gwarguy gwarguy is offline Offline
Newbie Poster

Re: Newbie Help

 
0
  #2
Mar 25th, 2008
edit limit expired. the = in the if statements are supposed to be ==
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 917
Reputation: linux is an unknown quantity at this point 
Solved Threads: 27
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Newbie Help

 
0
  #3
Mar 25th, 2008
You can do:
  1. if sex="M" or "m"

or and and
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 4
Reputation: Mike W is an unknown quantity at this point 
Solved Threads: 1
Mike W Mike W is offline Offline
Newbie Poster

Re: Newbie Help

 
0
  #4
Mar 25th, 2008
For a given string s, s.lower() will return the same string entirely in lower case. So, rather than

  1. if sex == "M":

you could use

  1. if sex.lower() == "m":
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 15
Reputation: gwarguy is an unknown quantity at this point 
Solved Threads: 0
gwarguy's Avatar
gwarguy gwarguy is offline Offline
Newbie Poster

Re: Newbie Help

 
0
  #5
Mar 26th, 2008
Thanks for the help guys, those worked wonderfully
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 15
Reputation: gwarguy is an unknown quantity at this point 
Solved Threads: 0
gwarguy's Avatar
gwarguy gwarguy is offline Offline
Newbie Poster

Re: Newbie Help

 
0
  #6
Mar 26th, 2008
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,074
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 299
woooee woooee is offline Offline
Veteran Poster

Re: Newbie Help

 
0
  #7
Mar 26th, 2008
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 666
Reputation: ZZucker is on a distinguished road 
Solved Threads: 38
ZZucker's Avatar
ZZucker ZZucker is offline Offline
Practically a Master Poster

Re: Newbie Help

 
1
  #8
Mar 26th, 2008
You cannot use the line:
  1. if sex == "M" or "m":
In Python you have to write it this way:
  1. if sex == "M" or sex == "m":
Actually you might just use:
  1. if sex in ["M", "m"]:
Last edited by ZZucker; Mar 26th, 2008 at 2:29 pm. Reason: code tags
Never argue with idiots, they'll just bring you down to their level and beat you with their experience.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 15
Reputation: gwarguy is an unknown quantity at this point 
Solved Threads: 0
gwarguy's Avatar
gwarguy gwarguy is offline Offline
Newbie Poster

Re: Newbie Help

 
0
  #9
Mar 26th, 2008
doing it like if body == "A" or body == "a"
all throughout the story, thanks for the help !
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 917
Reputation: linux is an unknown quantity at this point 
Solved Threads: 27
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Newbie Help

 
0
  #10
Mar 26th, 2008
Originally Posted by ZZucker View Post
You cannot use the line:
  1. if sex == "M" or "m":
In Python you have to write it this way:
  1. if sex == "M" or sex == "m":
Actually you might just use:
  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!
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 990 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC