Nov-20-2020, 09:30 AM
Hello, I am new to Python and I've been playing around with the 'Hello World' script that everyone probably starts with
I wanted to see if i could you 'if' properly so I made 2 'if' lines that would print a different response depending on 'age=input()'
I created a simple equation to write if the age=input() was <'18'. It simple finds the different between 'age=input()' and '18' to print how long is left until 18.
The trouble is, if 'age=input()' is a single digit int such as 4 or 9, the script will print the line for 'age>='18'. Almost like a single digit int is greater than '18'
Here is the code:
print('hello world!')
print('What is your name?')
name=input()
print('That is a nice name, '+name)
print('How old are you, '+name+' p.s. put a zero infront of a single digit')
age=input()
if age>='18':
print("if you're "+age+", that means it's beer o'clock!")
diff=str(18-(int(age)))
if age<'18':
print('That means you have to wait '+diff+' years to have a drink on me!')
Here is the output if age is a single digit int:
hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
2
if you're 2, that means it's beer o'clock!
Whereas is you put a '0' infront of the single digit int, it works:
hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
02
That means you have to wait 16 years to have a drink on me!
Any help is appreciated!
I wanted to see if i could you 'if' properly so I made 2 'if' lines that would print a different response depending on 'age=input()'
I created a simple equation to write if the age=input() was <'18'. It simple finds the different between 'age=input()' and '18' to print how long is left until 18.
The trouble is, if 'age=input()' is a single digit int such as 4 or 9, the script will print the line for 'age>='18'. Almost like a single digit int is greater than '18'
Here is the code:
print('hello world!')
print('What is your name?')
name=input()
print('That is a nice name, '+name)
print('How old are you, '+name+' p.s. put a zero infront of a single digit')
age=input()
if age>='18':
print("if you're "+age+", that means it's beer o'clock!")
diff=str(18-(int(age)))
if age<'18':
print('That means you have to wait '+diff+' years to have a drink on me!')
Here is the output if age is a single digit int:
hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
2
if you're 2, that means it's beer o'clock!
Whereas is you put a '0' infront of the single digit int, it works:
hello world!
What is your name?
G
That is a nice name, G
How old are you, G p.s. put a zero infront of a single digit
02
That means you have to wait 16 years to have a drink on me!
Any help is appreciated!