| | |
Newbie Help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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.
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
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.
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)
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
Python Syntax (Toggle Plain Text)
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.
Toshiba M115 ● 1.49 GB DDR-2 RAM ● 1.6 GHz Centrino Duo ● 80GB HDD ● Windows XP Media Center Edition
•
•
Join Date: Mar 2008
Posts: 4
Reputation:
Solved Threads: 1
For a given string s,
you could use
s.lower() will return the same string entirely in lower case. So, rather than python Syntax (Toggle Plain Text)
if sex == "M":
you could use
python Syntax (Toggle Plain Text)
if sex.lower() == "m":
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.
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.
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)
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.
Last edited by gwarguy; Mar 26th, 2008 at 11:36 am. Reason: text blocks look horrible when printing output
•
•
Join Date: Dec 2006
Posts: 1,074
Reputation:
Solved Threads: 299
•
•
•
•
Actualy that didnt work. I changed the statements to
if sex == "M" or "m":
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)
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"
Last edited by woooee; Mar 26th, 2008 at 1:36 pm.
You cannot use the line:
In Python you have to write it this way:
Actually you might just use:
python Syntax (Toggle Plain Text)
if sex == "M" or "m":
python Syntax (Toggle Plain Text)
if sex == "M" or sex == "m":
python Syntax (Toggle Plain Text)
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.
•
•
•
•
You cannot use the line:
In Python you have to write it this way:python Syntax (Toggle Plain Text)
if sex == "M" or "m":
Actually you might just use:python Syntax (Toggle Plain Text)
if sex == "M" or sex == "m":
python Syntax (Toggle Plain Text)
if sex in ["M", "m"]:
Thanks again!
Toshiba M115 ● 1.49 GB DDR-2 RAM ● 1.6 GHz Centrino Duo ● 80GB HDD ● Windows XP Media Center Edition
![]() |
Similar Threads
- As a newbie, where i should start from in linux? (Getting Started and Choosing a Distro)
- Questions about building a system (was: newbie) (Troubleshooting Dead Machines)
- Best free C/C++ compiler for a newbie? (C++)
- help newbie alert needs help with login page (ASP.NET)
- newbie needs help, basic mfc stuff (C++)
- Hello, newbie here... (Geeks' Lounge)
- Book For Newbie (C++)
- Newbie - how do I start C++ programming? (C++)
- PHP newbie, project feasibility (PHP)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the Python Forum
- Previous Thread: String problems1!
- Next Thread: Graphing Projectile Motion with wxPython
Views: 990 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive schedule screensaverloopinactive scrolledtext searchingfile server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






