I'm new to python.
I want the program to ask
"is Johnny hungry? True or false?"
user inputs True
then print is "Johnny needs to eat."
user inputs false
then print "Johnny is full."
I know to add an int I type in
johnnyHungry = int(input("Is johnny hungry "))
but I want them to enter True/false, not an int.
you can use a simple helper that will force whatever input you want
def get_bool(prompt):
while True:
try:
return {"true":True,"false":False}[input(prompt).lower()]
except KeyError:
print("Invalid input please enter True or False!")
print get_bool("Is Jonny Hungry?")
you can apply this to anything
def get_int(prompt):
while True:
try:
return int(input(prompt))
except ValueError:
print("Thats not an integer silly!")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With