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.

print "Please choose your hero"
sex = raw_input("(M)ale or (F)emale: ")
if sex = "M":
    print "A young and adventerous lad out to save the world!"
else:
    print "Look out Lara Croft, there is a new show in town"
    
print "_"*100
print " "
print "Now for your body build"
body = raw_input("Are you (S)lim, (A)verage, (M)uscular, or (H)eavy")

if body = "S":
    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

if sex = "M", "m"
                        ["M", "m"]
                        {"M", "m"}
                         ["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.

Recommended Answers

All 9 Replies

edit limit expired. the = in the if statements are supposed to be ==

You can do:

if sex="M" or "m"

or and and

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

if sex == "M":

you could use

if sex.lower() == "m":

Thanks for the help guys, those worked wonderfully

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.

print "Please choose your hero"
sex = raw_input("(M)ale or (F)emale: ")
if sex = "M" or "m":
    print "A young and adventerous lad out to save the world!"
else:
    print "Look out Lara Croft, there is a new show in town"
    
print "_"*100
print " "
print "Now for your body build"
body = raw_input("Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: ")

if body == "S" or "s":
    print "Thin and wiry, your a willow in the wind"
elif body == "A" or "a":
    print "You could pass by him and never notice him, or his gun."
elif body == "M" or "m":
    print "Tall and hulking, you wouldnt want to run into him in a dark alley"
else: 
    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.

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.

literal_list = [ ["Please choose your hero", "(M)ale or (F)emale: "],
                 ["Now for your body build", "Are you (S)lim, (A)verage, (M)uscular, or (H)eavy: "]]
##           dictionary key=M+0 is necessary because you also use M for Muscular
##           The zero relates to the zero element of literal_list=Male
##           M+1 is the "1" element of literal_list=Muscular
answer_dic ={"M0":"A young and adventerous lad out to save the world!",
             "F0":"Look out Lara Croft, there is a new show in town",
             "S1":"Thin and wiry, your (you're) a willow in the wind",
             "A1":"You could pass by him and never notice him, or his gun.",
             "M1":"Tall and hulking, you wouldnt want to run into him in a dark alley",
             "H1":"Hey, big people need love too!"}

j=0
stop=len(literal_list)
while j < stop:
   print "j =", j     ## for testing
   print literal_list[j][0]
   answer = raw_input(literal_list[j][1])
   key = answer.upper() + str(j)
   print "the key is", answer.upper(), str(j), key   # to see what is going on
   if key in answer_dic:
      print answer_dic[key], "\n"
      j += 1
   else:
      print answer, "is not a correct response...try again"

You cannot use the line:

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

In Python you have to write it this way:

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

Actually you might just use:

if sex in ["M", "m"]:
commented: Thanks for explaining my problem in a clear manner as fit to my little or no experience:) +1

doing it like if body == "A" or body == "a"
all throughout the story, thanks for the help !

You cannot use the line:

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

In Python you have to write it this way:

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

Actually you might just use:

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.