No mather what input I give I get the output "I think you're to old to go on an adventure. Your adventure ends here."
Like if I input 17 I get "I think you're to old to go on an adventure. Your adventure ends here." and if I input 18-59 I get "I think you're to old to go on an adventure. Your adventure ends here."
while True:
age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
if age.isdigit() == False:
print "\n Please, put only in numbers!"
time.sleep(3)
elif age < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif age >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
You need to compare your input as an int
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
Otherwise you are comparing a str to an int. See the following example
>>> 5 < 10
True
>>> type(5)
<type 'int'>
>>> '5' < 10
False
>>> type('5')
<type 'str'>
In Python 2.x, comparing values of different types generally ignores the values and compares the types instead. Because str >= int, any string is >= any integer. In Python 3.x you get a TypeError instead of silently doing something confusing and hard to debug.
The problem is that raw_input always returns a string object, and those don't really compare to int types. You need to do some type conversion.
If you want to use isdigit to test if the input is a number, then you should proceed this way:
while True:
age = raw_input("\n So tell me " + str(name) + ", how old are you?: ")
if age.isdigit() == False:
print "\n Please, put only in numbers!"
time.sleep(3)
elif int(age) < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif int(age) >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
However, you can simplify the code a little by converting to an integer right away, and just catching the exception if it's an invalid string:
while True:
try:
age = int(raw_input("\n So tell me " + str(name) + ", how old are you?: "))
if age < 18:
print "\n %s. A minor. You got to be atleast 18 to go on this adventure. Your adventure ends here." % age
time.sleep(7)
exit(0)
elif age >= 60:
print "\n %s. I think you're to old to go on an adventure. Your adventure ends here." % age
time.sleep(5)
exit(0)
else:
print "\n %s. You're starting to get old." % age
break
except ValueError:
print "\n Please, put only in numbers!"
time.sleep(3)
Related
Beginner question:
How do I have my input code ignore certain strings of text?
What is your name human? I'm Fred
How old are you I'm Fred?
How do I ignore user input of the word "I'm"?
How old are you I'm Fred? I'm 25
Ahh, Fred, I'm 25 isn't that old.
How do I again ignore user input when dealing with integers instead?
This is the code I have so far:
name = input("What is your name human? ")
while True:
try:
age = int(float(input("How old are you " + name + "? ")))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
You could use .replace('replace what','replace with') in order to acheive that.
In this scenario, you can use something like:
name = input("What is your name human? ").replace("I'm ","").title()
while True:
try:
age = int(input("How old are you " + name + "? ").replace("I'm ",""))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
Similarly, you can use for the words you think the user might probably enter, this is one way of doing it.
Why not use the list of words that should be ignored or removed from the input, i suggest you the following solution.
toIgnore = ["I'm ", "my name is ", "i am ", "you can call me "]
name = input("What is your name human? ")
for word in toIgnore:
name = name.replace(word, "")
while True:
try:
age = int(float(input("How old are you " + name + "? ")))
if age >= 50:
print(str(age) + "! Wow " + name + ", you're older than most of the oldest computers!")
else:
print ("Ahh, " + name + ", " + str(age) + " isn't that old.")
break
except ValueError:
print("No, I asked how old you were... ")
print("Let us try again.")
The output is following
I'm a new programmer and just starting off with Python. I have the following 2 questions, however, I decided to put them in one post.
When asking to input age, how do I force the program to only accept numbers?
The concept is that after the user has entered their age, the program would pick a random number between 1 and 100 and compare it to the user input, returning either "I'm older than you", "I'm younger than you" or "we are the same age".
# Print Welcome Message
print("Hello World")
# Ask for Name
name = input("What is your name? ")
print("Hello " + str(name))
# Ask for Age
age = input("How old are you? ")
print("Hello " + str(name) + ", you are " + str(age) + " years old.")
random.randint(1, 100)
Try the following
import random
# Print Welcome Message
print("Hello World")
# Ask for Name
name = input("What is your name? ")
print("Hello " + str(name))
# Ask for Age
while True: # only numbers
try:
age = int(input("How old are you? "))
except:
pass
print("Hello " + str(name) + ", you are " + str(age) + " years old.")
t=random.randint(1, 100)
if t==age:
print("we are the same age") #compare ages
if t<age:
print("I'm younger than you")
if t>age:
print("I'm older than you")
Hi you can try this simply.
import random
name = input("What is your name? ")
print("Hello " + str(name))
while True :
try :
age = int(input("How old are you? "))
break
except :
print("Your entered age is not integer. Please try again.")
print("Hello " + str(name) + ", you are " + str(age) + " years old.")
randNumber=random.randint(1, 100)
if randNumber > age :
print("I am older than you")
if randNumber < age :
print("I am younger than you")
else :
print("we are the same age")
Only made few changes to existing code with modifications asked.
import random
# Print Welcome Message
print("Hello World")
# Ask for Name
name = input("What is your name? ")
print("Hello " + str(name))
# Ask for Age
while True:
try:
age = int(input("How old are you? "))
except ValueError:
pass
print("Hello " + str(name) + ", you are " + str(age) + " years old.")
my_random = random.randint(1, 100)
if my_random > age:
print("Im older than you")
elif my_random < age:
print("I'm younger than you")
else:
print("We are the same age")
Includ a try block around the age part. If the user inputs an non-int answer then it will just pass. I then saved the random int that you generated it and compared it to the age to find if the random int was greater than the age.
To answer your first question, use int() as such:
age = int(input("How old are you? "))
This will raise an exception (error) if the value is not an integer.
To answer your second question, you may store the random number in a variable and use it in comparison to the user's age, using conditional statements (if, elif, else). So for instance:
random.seed() # you need to seed the random number generator
n = random.randint(1, 100)
if n < age:
print("I am younger than you.")
elif n > age:
print("I am older than you.")
else:
print("We are the same age.")
I hope this answers your question. You can refer to the official Python docs for more info on conditional statements.
My guess is that you should convert the variable age into integer. For example:
age = input("How old are you?")
age = int(age)
This should work.
The answer to the 1st question.
x = int(input("Enter any number: "))
To force the program to enter number add int in your input statement as above.
print("Hello there")
name=input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
print("K")
else:
print("Sorry bruh")
print("Thanks")
keeps showing me "Sorry bruh" at the end when entered 15. Why? What's wrong?
Cast your input to int:
age = int(input("Your age"))
You could add a try-except. Your condition should equally be evaluated on or not and
Either you use Python3 and you need to cast with int(input("Your age")) or you use Python2 and then you need to use raw_input to read the name: name=raw_input("What is your name?")
Ok , so you can try this code in python2.7 and it will work as you desired:
print("Hello there")
name=raw_input("What is your name?")
print("Welcome to the some game, " + name + "!")
print("I'm going to ask you some basic questions so that we could work together")
age=input("Your age")
if age >= 14 and age < 41:
print("K")
else:
print("Sorry bruh")
print("Thanks")
Ok, I'm gonna explain this as best I can.
I am trying to make an if/else statement in python that basically informs the user if they put in the right age based on raw input, but I keep getting an error in terms of my if statement. Can I get some help?
from datetime import datetime
now = datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"
name = raw_input("What is your name?")
age = raw_input("How old are you?")
if age == 1 and <= 100
print "Ah, so your name is %s, and you're %s years old. " % (name, age)
else:
print " Yeah right!"
change your if statement to this:
if age >=1 and age <=100:
two things:
if age >=1 and <=100 is missing age <=100
you are missing a : at the end
Here is the formatted and corrected code:
import datetime
now = datetime.datetime.now()
print '%s/%s/%s %s:%s:%s' % (now.month, now.day, now.year, now.hour,now.minute, now.second)
print "Welcome to the beginning of a Awesome Program created by yours truly."
print " It's time for me to get to know a little bit about you"
name = raw_input("What is your name?")
age = raw_input("How old are you?")
while int(age) < 1 or int(age) > 100:
print " Yeah right!"
age = raw_input("How old are you?")
print "Ah, so your name is %s, and you're %s years old. " % (name, age)
Also, double check indents and your logic for the age.
Making a "Mad Libs" style thing in Python for class, it's my first day, I've already picked up most of what I needed to know, but I'm not sure how to go about using the "if", "elif", "else" things. Here's what I have so far, basically, when the age is input I want it to choose if the person is an adult or a kid.
print "welcome to your short story"
name = raw_input("Input your name: ")
age = raw_input("Input your age: ")
if age > 21:
age = "adult"
elif age < 21:
age = "kid"
print "My name is ",name,"and I am a " ,age,"year old ",age
1) You are overriding the inputted age with strings adult of kid.
2) You have to handle the case when age equals 21.
3) The age input needs to be converted to an integer.
Let's rewrite your code, and see how we can improve:
print "welcome to your short story"
name = raw_input("Input your name: ")
# Convert the input to an integer.
age = int(raw_input("Input your age: "))
# This is the status variable being either adult or child
# before you were overriding age variable with adult or kid
status = ""
# Also, you have to handle the case where the age equals 21, before
# you were just checking if it is less or greater than 21
if age >= 21:
status = "adult"
elif age < 21:
status = "kid"
print "My name is ", name ," and I am a " , age ," year old " , status
I realized I needed a different value and have changed it.
print "welcome to your short story"
name = raw_input("What is your name?: ")
age = raw_input("How old are you?: ")
sex = raw_input("Are you a boy or a girl?: ")
if age > 21:
targetAge = "adult"
elif age < 21:
targetAge = "kid"
print "My name is ",name,"and I am a " ,age,"year old ",targetAge,"."
So, basically, when it is printed, it should read as "My name is _____ and I am a __ year kid/adult." Depending on the number they've input. The reason I'm not using the int() function is because that was never mentioned, this is literally the first day of class, so I'm going by what the instructor has gone over.
As suggested by foxygen, the int function is needed to convert your input from a string to an int
age = int(raw_input("Input your age: "))
There is a difference between the characters "1", "2", "3" and so forth, and the numbers 1, 2, 3, and so forth. Some languages try to convert for you, but python will not do this, so you end up comparing "32" to 21, which is an apples-to-oranges comparison.
Also, while you're safe in this particular instance, you'll notice how you're reassigning age while you're still computing based on its original value. This is not generally safe, and you would be better off assigning to a new variable:
if age > 21:
age_label = "adult"
elif age < 21:
age_label = "kid"
else:
age_label = "person" # in case a 21 year old uses your program
You're safe in this case because the if/else construction will only ever execute one branch, but it's best not to get in the habit of rewriting the input value while you're still consulting it.
print "Welcone to your Story"
name = raw_input("Please enter yout name: ")
age = int(raw_input("please enter your age: "))
if age >= 21:
status = "an adult"
else:
#enter code here
status = "a kid"
print "My name is %r , I am %r and I am %r years old" %(name,status,age)