creating a holiday programme? - python

I have been set a task and it is the following, I was wondering would my code would work?
write a program to ask for a name and an age.
when both values have been entered, check if the person is the right
age to on an 18-30 holiday(they must be over 18 and under 31
name = input("What is your name? ")
age = int(input("How old are you ? "))
if 18 <= age < 31:
print("Hello",name,"welcome to this holiday.".format(name))
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")

well actually it works but you don't need the format part
name = input("What is your name? ")
age = int(input("How old are you ? "))
if age >=18 and age <31:
print("Hello",name,"welcome to this holiday.")
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")
if you want to use format you can do this
print("Hello {name} welcome to this holiday.".format(name=name))

Code works perfect, but you have a useless format
Just use format, not the , part.
name = input("What is your name? ")
age = int(input("How old are you ? "))
if 18 <= age < 31:
print("Hello {name} welcome to this holiday.".format(name))
else:
print("Sorry {name} you are not old enough to go on this holiday.".format(name))
Or if you don't like to do format
name = input("What is your name? ")
age = int(input("How old are you ? "))
if age >=18 and age <31:
print("Hello",name,"welcome to this holiday.")
else:
print("Sorry",name,"you are not old enough to go on this holiday. ")

Related

what is wrong with python my python code in while loop?

I don't know what is the error of my code can anyone help me to fix this code.
age = ""
while int(len(age)) == 0:
age = int(input("Enter your age: "))
print("Your age is " + age)
You wanted a string for age but you transformed this age into an integer inside your loop :)
You can write :
age = ""
while age == "":
age = input("Enter your age: ")
print("Your age is " + age)
In Python, "" and 0 (and values like None and empty containers) are considered "falsey". Not necessarily the same thing as False but logically treated like False.
So you can simplify your while loop:
age = ""
while not age:
age = input("Enter your age: ")
print("Your age is " + age)
You cast age to be an int inside of your while loop, while using the len(age) as the comparison.
int does not have a length.
You want to wait to cast age until you are outside of the loop, or in this situation, don't cast it at all.
age = ""
while len(age) == 0:
age = input("Enter your age: ")
print("Your age is " + age)

Why does (BDate) is shown as syntax error

enter image description hereenter image description here
I tried everything I can It doesn't work pls help
print ("Enter today's date:")
DateToday = input()
print ("Enter your birthday {yyyy/mm/d)"
BDate = input()
Age = DateToday - BDate
print Age.year
I'm assuming you want just the year for age?
in which case you would do:
datetoday = int(input("Enter the current year: "))
bdate = int(input("Enter your birth year: "))
age = datetoday - bdate
print ("Your age is: " + str(age))
You forgot to add a few brackets and are asking for unnecessary data. This will give how many years old a person is.

Create a vector given N input of names and ages python

I need to create a program that takes as inputs multiple entries of names and ages. Then I want it to return the names and ages of those entries that have higher age values than the average age of all entries.
For example:
input( "Enter a name: ") Albert
input( "Enter an age: ") 16
input( "Enter a name: ") Robert
input( "Enter an age: ") 18
input( "Enter a name: ") Rose
input( "Enter an age: ") 20
The average is = 18
Rose at 20 is higher than the average.
How can I do this?
answers = {}
# Use a flag to indicate that the questionnaire is active.
questions_active = True
while questions_active:
# Ask for the person's name and response.
name = raw_input("\nEnter a name: ")
response = raw_input("Enter an age: ")
# Store the response in the dictionary:
answers[name] = int(response)
# Check if anyone else is going to take the questionnaire.
repeat = raw_input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
questions_active = False
average_age = sum(answers.values())/float(len(answers))
print("The average is " + str(average_age))
# Questionnaire is complete. Show the results.
for name, response in answers.items():
if response > average_age:
print(name.title() + " at " + str(response) + " is higher than the average.")
This answer is based on a similar example from the book "Python Crash Course: A Hands-On, Project-Based Introduction to Programming" https://www.nostarch.com/pythoncrashcourse

Simple Python "if", "elif", "else"

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)

Simple raw_input and conditions

I created the simple code:
name = raw_input("Hi. What's your name? \nType name: ")
age = raw_input("How old are you " + name + "? \nType age: ")
if age >= 21
print "Margaritas for everyone!!!"
else:
print "NO alcohol for you, young one!!!"
raw_input("\nPress enter to exit.")
It works great until I get to the 'if' statement... it tells me that I am using invalid syntax.
I am trying to learn how to use Python, and have messed around with the code quite a bit, but I can't figure out what I did wrong (probably something very basic).
It should be something like this:
name = raw_input("Hi. What's your name? \nType name: ")
age = raw_input("How old are you " + name + "? \nType age: ")
age = int(age)
if age >= 21:
print "Margaritas for everyone!!!"
else:
print "NO alcohol for you, young one!!!"
raw_input("\nPress enter to exit.")
You were missing the colon. Also, you should cast age from string to int.
Hope this helps!
With python, indentation is very important. You have to use the correct indentation or it won't work. Also, you need a : after the if and else
try:
if age >= 21:
print #string
else:
print #other string
Firstly raw_input returns a string not integer, so use int(). Otherwise the if-condition if age >= 21 is always going to be False.:
>>> 21 > ''
False
>>> 21 > '1'
False
Code:
name = raw_input("Hi. What's your name? \nType name: ")
age = int(raw_input("How old are you " + name + "? \nType age: "))
The syntax error is there because you forgot a : on the if line.
if age >= 21
^
|
colon missing

Categories

Resources