Reputation: 37
I am new to coding in Python. I am trying to make my code so that if I enter the age a series of text will be printed. However, my code only works if i follow it line by line. For example, when i input the age 2000+ immediately nothing will happen. I need to first input an integer less than 12, followed by an integer over 2000.
print('Please input name')
if input() == 'Alice':
print('Hi, Alice.Please input age')
if int(input()) < 12:
print('You are not Alice, kiddo.')
elif int(input()) > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif int(input()) == 100:
print('You are not Alice, grannie.')
elif 12 < int(input()) < 99:
print('You are Alice!.')
Upvotes: 0
Views: 973
Reputation: 11
Hopefully, this is what you are looking for:
while True:
name = input("Please ENTER your name: ")
if name == "Alice":
print("Hi Alice!")
break
print("Sorry, your name isn't correct. Please re-enter.")
age = False
while age != True:
age = int(input("Please ENTER your age: ")
age = True
if age < 12:
print("You're not Alice, kiddo.")
age = False
elif age > 2000:
print("Unlike you, Alice is not an undead, immortal vampire.")
age = False
elif age == 100:
print("You're not Alice, Granny!")
age = False
else:
print("You are Alice!")
age = True
Upvotes: 0
Reputation: 645
Here I wrote code for your understanding purpose. Take new variable, so that no need to repeat input() method several times. Also, Age validation code keeps inside the first condition and it will be executed when the 1st condition will be true.
print('Please input name')
var = input()
if var == 'Alice':
print('Hi, Alice.Please input age')
var = input()
try:
if int(var) < 12:
print('You are not Alice, kiddo.')
elif int(var) > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif int(var) == 100:
print('You are not Alice, grannie.')
elif 12 < int(var) < 99:
print('You are Alice!.')
except Exception as ex:
print('Invalid Data: Error: ' + ex)
else:
print ("Invalid Name")
Upvotes: 3
Reputation: 31
var = input('Please input name ')
if var == 'Alice':
var = int(input('Hi, Alice.Please input age '))
if var < 12:
print('You are not Alice, kiddo.')
elif var > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif var == 100:
print('You are not Alice, grannie.')
elif 12 < var < 99:
print('You are Alice!.')
else:
print ("Invalid Name")
This code works because it asks one time and tries to see if some conditions are true, instead of asking each time.
Upvotes: 3
Reputation: 5241
The input()
function returns a string. Quoting the docs (emphasis mine):
The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.
So, in each if
when you call input()
, you have to enter a new string. Thus, you have to first enter an integer below 12.
To fix this problem, you need to store the original input in a variable. Now, as the docs say, input()
returns a string. So, either you can cast (using int()
) the integer in each case, by doing:
if int(age) < 12:
and storing the variable as a string.
Though, unless you do not have any specific reason to keep the age as a string, I'd recommend you to convert the string while storing the age in the variable in the first place:
age = int (input())
In this case, age
will have an int.
Upvotes: 1
Reputation: 3818
input
is invoked every time when followed by ()
. So the multiple input()
's in the if
elif
's are not necessary.
store the result of input()
like age = int(input())
, then use age
in the if
and elif
parts instead.
Upvotes: 1
Reputation: 1415
print('Please input name')
if input() == 'Alice':
print('Hi, Alice.Please input age')
age = int(input()) # take input and assign it on a variable
if age < 12:
print('You are not Alice, kiddo.')
elif age > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif age == 100:
print('You are not Alice, grannie.')
elif 12 < age < 99:
print('You are Alice!.')
Upvotes: 1
Reputation: 8378
Every time you go to another branch in you if
you are asking user to enter another age! Instead do the following:
age = int(input())
if age < 12:
print('You are not Alice, kiddo.')
elif age > 2000:
print('Unlike you, Alice is not an undead, immortal vampire.')
elif age == 100:
print('You are not Alice, grannie.')
elif 12 < age < 99:
print('You are Alice!.')
Upvotes: 1