Variable inputs within an IF creating a bank. - python

I'm new to Python and I am currently doing some work with IF statements.
This is what I have so far...
print("Hello")
myName = input("What is your name?")
print("Hello " +myName)
myAge = int(input("How old are you?"))
if myAge <=18:
myResponse = input("You must still be at school?")
if myResponse == "Yes" or "yes" or "YES" or "Y" or "yEs" or "y":
mySchool = input("What school do you go to?")
print (mySchool, "that is a good school I hear")
if myResponse == "No" or "n" or "N" or "NO":
print("Lucky you, you have lots of free time!")
if myAge >=19:
myResponse = input("You must have a job?")
if myResponse == "Yes" or "yes" or "YES" or "Y" or "yEs" or "y":
myWork = input("What do you do?")
print (myWork, "Thats a tough job")
if myResponse == "No" or "n" or "N" or "NO":
print("Lucky you, you have lots of free time!")
I want the user to be able to answer a question with a one word answer however have various options that would be recognized by the program for example "No", "NO" and "no" or "yes", "YES" and "Yes".
I have just figured out this way of doing it seen above but is there a better way it should be done?
Bare in mind I am new to this so it is probably a silly question.
Any help would be greatly appreciated.
Si

This condition checks that myRespone is either yes or "y" and is case insensitive (meaning that yes, YeS, and others are all valid)
myResponse.lower() in ["yes","y"]

The question asks specifically for answers in the form "yes" or "no" with different capitalizations ("y" or "n" are not mentioned). With that in mind, we can do as follows, being careful to remove any extra spaces:
if myresponse.strip().lower() == "yes":
# if yes, do something
And similarly:
if myresponse.strip().lower() == "no":
# if no, do something else

Try this:
if myResonse.lower() == "yes":
etc

With string functions:
if myResponse.upper() == 'NO':
# do something
Or:
if myResponse.lower() == 'no':
#do something

Related

Raw Input Inside If-statement?

Is there a way to use raw input inside an if-statement? I would like to ask the user a question and if they type "yes" I want to code to continue working after the if-statement. If they type "no" I want the code to say "Thank you for your time" and then stop all actions after that particular if-statement. Is this possible?
Code (I have never done this before so this is a wild guess):
tri=raw_input("Do the points that you entered form a triangle? (yes or no)")
tri=str(tri)
if tri == "yes" or "Yes" or "YES":
print "Your triangle is an:"
elif tri == "no" or "NO" or "No":
print "Thank you for your time."
else:
print "Not a valid answer, please try again later."
Give this a shot:
tri = None
while tri not in ['yes', 'no']:
tri=raw_input("Do the points that you entered form a triangle? (yes or no): ").lower()
if tri == 'yes':
print "Your triangle is an:"
else:
print "Thank you for your time."

if/elif statements not working in while loop [duplicate]

This question already has answers here:
Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?
(8 answers)
Closed 7 years ago.
I'm writing a simple 8ball response program and have an issue. When I run this program but give any option other than "y" or "yes" in response to the variable 'rd', the program thinks I have actually inputted "yes" and goes ahead with the code indented in the 'if' statement with a yes response. Why is this? I can't work out why.
import time
import random
import sys
resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
"In your dreams!", "Without a doubt!", "Most likely!",
"Very doubtful!", "I'm going to have to say no this time!",
"What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
"Better not tell you now", "I've been told by to tell you no...",
"I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
"You wish!", "All signs point to no!", "All signs point to yes!",
"If you truly believe it!"
]
def intro():
print "Hello! Welcome to 8 Ball!\n"
time.sleep(2)
def main():
quit = 0
while quit != "n":
rd = raw_input("Are you ready to play? Enter y/n: ")
if rd.lower() == "y" or "yes":
question = raw_input("\nType your question and please press enter: ")
print "\n"
print random.choice(resp)
print "\n"
quit = raw_input("Do you want to roll again? Enter y/n: ")
elif rd.lower() == "n" or "no":
print "Looks like you need some more time to think. Have a few seconds to think about it.."
time.sleep(3)
quit = raw_input("Are you ready to play now? Enter y/n: ")
else:
print "That wasn't an option. Try again."
rd = raw_input("Are you ready to play? Enter y/n: ")
print "Okay! Thanks for playing."
intro()
main()
>>> bool("yes")
True
"yes" evaluates to true
if rd.lower() in ("y", "yes"):
could be used check to see if the value is 'y' or 'yes'
You can't do if x == a or b in python you have to do x == a or x == b or x in (a, b)
import time
import random
import sys
resp = ["Yes!", "No!", "Maybe!", "Don't be so silly!",
"In your dreams!", "Without a doubt!", "Most likely!",
"Very doubtful!", "I'm going to have to say no this time!",
"What kind of a question is that? Of course not!", "I reckon there's a 20% chance!",
"Better not tell you now", "I've been told by to tell you no...",
"I've been told to tell you yes...", "It's possible!", "More likely to see pigs fly!",
"You wish!", "All signs point to no!", "All signs point to yes!",
"If you truly believe it!"
]
def intro():
print "Hello! Welcome to 8 Ball!\n"
time.sleep(2)
def main():
quit = 0
while quit != "n":
rd = raw_input("Are you ready to play? Enter y/n: ")
if rd.lower() in ("y", "yes"):
question = raw_input("\nType your question and please press enter: ")
print "\n"
print random.choice(resp)
print "\n"
quit = raw_input("Do you want to roll again? Enter y/n: ")
elif rd.lower() in ("n", "no"):
print "Looks like you need some more time to think. Have a few seconds to think about it.."
time.sleep(3)
quit = raw_input("Are you ready to play now? Enter y/n: ")
else:
print "That wasn't an option. Try again."
rd = raw_input("Are you ready to play? Enter y/n: ")
print "Okay! Thanks for playing."
intro()
main()
You can't do this:
if rd.lower() == "y" or "yes":
because it's evaluating "yes" by itself. Instead try:
if rd.lower() == "y" or rd.lower() == "yes":
also consider:
if rd.lower() in ["y", "yes"]:

Why does this while loop not work?

This loop keeps looping even if I enter "no" and when I type "jdlfjap", for example, it continues to loop without a "?".
Does anyone know why this is?
def makeContact():
contactName = input("Name: ")
contactNumber = input("Number: ")
dictionaryForContacts[contactName] = contactNumber
def continueMaking():
while True:
continueMaking = input("\nWould you like to continue making contacts? ")
if continueMaking == "Yes" or "yes" or "YES":
makeContact()
continue
elif continueMaking == "No" or "no" or "NO":
break
else:
print ("?")
continue
The statement if continueMaking == "Yes" or "yes" or "YES": is equivalent to (continueMaking == "Yes") or "yes" or "YES": which, regardless of the value of continueMaking returns the string "YES", which is truthy and thus the makeContact call always executes. Case-insensitive string comparisons can be accomplished by continueMaking.lower() == "yes".
Overwriting the function continueMaking with the variable continueMaking adds confusion. Choose a different variable name. Readability counts.

How to make python accept random input

I'm currently working on a python project and I need it to accept random input from users at some point.
So, if I have, for example:
def question_one () :
answer_one = raw_input ('How many days are there in a week? ').lower()
try:
if answer_one == 'seven' or answer_one == '7' :
question_2()
Everything works wonders. But how can I make python accept random input, as in
def question_two () :
answer_two = raw_input ('What´s your mother´s name? ').lower()
try:
if answer_two == ***I have no idea how to code this part*** :
question_3()
In this case, I would need python to accept any input and still take the user to the next question. How could I do it?
Just remove the if clause then.
If the input doesn't have to be of a specific form, or have some particular property, then there's no need for the if statement, or even the try.
def question_two():
answer_two = raw_input("What's your mother's name?").lower()
question_3()
If you want to be able to re-ask the question, if they don't get it right then you can loop back to it like so. The only acceptable answer for this question is "yes", or "YES", etc..
If they don't answer correctly it will ask them again,till they get it right.
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
question2()
else:
question1()
If you want them to be able to go on to the next question even if they get it wrong, you can do like so:
def question1():
answer1 = raw_input("Do you like chickens?")
answer1 = answer1.lower()
if answer1 == 'yes':
print "That is Correct!"
else:
print "Next question coming!"
question2()
def question2():
answer2 = raw_input("How many days in a week?")
answer2 = answer2.lower()
if answer2 == '7' or answer2 == "seven":
print "That is Correct!"
else:
print "Sorry,that was wrong"
question3()

Function with if statements

I don't understand why this function doesn't print the text I want it to print, what could I change?
def shut_down(answer):
if answer == "Yes" or "YES" or "yes":
print "Shutting down..."
return "Shutting down..."
if answer == "No"or "NO" or "no":
print "Shutdown aborted!"
return "Shutdown aborted!"
else:
print "Sorry, I didn't understand you."
return "Sorry, I didn't understand you."
shut_down(yes)
I guess the language is Python. The or operator operate between conditions, so you need to do
answer == "yes" OR answer == "YES"
etc ....
Your answer == "Yes" Or "YES" is always true because "YES" is considered as an true condition.
if answer.lower() == 'yes':
That's how you should do it. Actually, do this: if answer.lower() in ('yes', 'y'): to make it more convenient for lazy people like myself.
Anyway, the proper way to check for a list of strings is this:
if answer in ('foo', 'bar', 'foobar'):
The following is not correct (the body of the if always executes):
if answer == "Yes" or "YES" or "yes":
Instead, write
if answer in {"Yes", "YES", "yes"}:
or
if answer.lower() == "yes":
You want to change it to this:
if answer == "Yes" or answer == "YES" or answer == "yes":
print "Shutting down..."
return "Shutting down..."
Or even tidier:
if answer in ["Yes", "YES", "yes"]:
or even moar tidier:
if answer.lower() == "yes":
You code probably has an error because you do not have the variable yes defined anywhere.
Define it, and then you should be good.
I am talking about the variable you are passing to your shut_down function

Categories

Resources