Create a vector given N input of names and ages python - 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

Related

creating a holiday programme?

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. ")

Im trying to make a simple code where you enter your name and age and it will Say is this information correct but idk how to do the correct thingg

print ("Enter your details to create your account ")
Username = input("Enter your username " )
age = input("Enter your Age ")
print("Your username is " + Username)
print("Your age is "+ age)
This is my code, but I'm not sure how to do the "is this information correct" thing.
You were almost there. Something like this should be added to your code and you'll be done.
correct = input("Is this information correct? (y/n)")
And this correct variable will hold a value of "y" or "n" which u can check later in your code if you want.
Just add another prompt at the end like:
data = input("Is this information correct?")
if data.lower() in ['y','ye','yes','yep','yeah']:
# YES
elif data.lower() in ['n','no','nope']:
# No
else:
# Invalid input
One line solution is
verified = True if input("Is this information correct? (y/n)").lower() == 'y' else False
If y or Y entered, verified become True, otherwise False.
EDIT
If you need a loop and get values while not verified by user as you mention in comment, you can use
verified = False
while not verified:
print("Enter your details to create your account ")
username = input("Enter your username: ")
age = input("Enter your Age ")
print("Your username is " + username)
print("Your age is " + age)
verified = True if input("Is this information correct? (y/n)").lower() == 'y' else False

loop questions Python

I need to ask a user how many people are booking in , max 8 people then take that amount and ask for user 1 details, user 2 details etc.save details to be printed later.Not sure what to I'm very new to python.
manager_name = raw_input("please enter managers Name: ")
email_address = raw_input("please enter your Email Address: ")
phone_number = raw_input("please enter your Phone number: ")
print("-----------------------------------------------------")
print ("Our Stuido can Accommodate up to max 8 musicians")
print("-----------------------------------------------------")
amount_of_band_members = int(raw_input("please enter the amount of band members"))
values = []
for i in range(amount_of_band_members):
values.append(int(input('Please enter Muscians Names & Insterments: ')))
manager_name = raw_input("please enter managers Name: ")
email_address = raw_input("please enter your Email Address: ")
phone_number = raw_input("please enter your Phone number: ")
print("-----------------------------------------------------")
print ("Our Stuido can Accommodate up to max 8 musicians")
print("-----------------------------------------------------")
amount_of_band_members = int(raw_input("please enter the amount of band members"))
if amount_of_band_members >8:
print ("Sorry we can only Accommodate maxuim 8 Musicians")
else :
print raw_input("please enter musicians and insterments")
while amount_of_band_members <8:
print raw_input("please enter next name")
amount_of_band_members +=1

Average of marks for three topics

I am trying to create a program that will ask the user for a username and password. If the login details are correct, the program should ask for the students name and then ask for three scores, one for each topic. The program should ask the user if they wish to enter another students details. The program should output the average score for each topic. I cannot work out how to enter the student marks for each topic per student and also how to work out the average for each topic for the class.
Can you please help?
login="teacher"
password="school"
usrnm=input("Please enter your username: ")
pw=input("Please enter your password: ")
if (usrnm==login) and (pw==password):
print("==Welcome to the Mathematics Score Entry Program==")
print("Do you want to enter the students score? Yes/No: ")
option = input()
option = option.title()
student_info = {}
student_data = ['Topic 1 : ', 'Topic 2 : ', 'Topic 3 : ']
while (option != "No"):
student_name = input("Name: ")
student_info[student_name] = {}
score1 = int(input("Please enter the score for topic 1: "))
student_info[student_name][Topic_1] = score1
score2 = int(input("Please enter the score for topic 2: "))
student_info[student_name][Topic_2] = score2
score3 = int(input("Please enter the score for topic 3: "))
student_info[student_name][Topic_3] = score3
print("Do you want to enter the students score? Yes/No: ")
option = input()
option = option.title()
average = sum(student_info.values())/len(student_info)
average = round(average,2)
print ("The average score is ", average)
else:
print("Access denied!")
just keep the marks seperate from the student names
students = []
marks = []
option = ""
while (option != "No"):
students.append(input("Name"))
marks.append([float(input("Mark_Category1:")),
float(input("Mark_Category2:")),
float(input("Mark_Category3:"))])
option = input("Add Another?")
import numpy
print(numpy.average(marks,0))
if you really want to do it without numpy
averages = [sum(a)/float(len(a)) for a in zip(*marks)] # transpose our marks and average each column

Python Phonebook program

I have this program, and it is almost perfect but I need the dictionary to print on separate lines like so:
Please enter a name (or just press enter to end the input): Tom
Please enter Tom's phone: 555-5555
Please enter a name (or just press enter to end the input): Sue
Please enter Sue's phone: 333-3333
Please enter a name (or just press enter to end the input): Ann
Please enter Ann's phone: 222-2222
Please enter a name (or just press enter to end the input):
Thank you.
Your phonebook contains the following entries:
Sue 333-3333
Tom 555-5555
Ann 222-2222
Here is my code:
def main():
phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
number = input("Please enter number: ")
phoneBook[name] = number
name = input("Please enter a name(or press enter to end input): ")
if name == '':
print("Thank You!")
print("Your phonebook contains the following entries:\n",phoneBook)
main()
Loop through the entries in your phonebook and print them one at a time:
for name, number in phoneBook.items():
print ("%s %s" % (name, number))
something like this:
strs = "\n".join( " ".join((name,num)) for name,num in phoneBook.items() )
print("Your phonebook contains the following entries:\n",strs)
if you dont want to write codes yourself, pprint could be an option:
import pprint
....
print("Your phonebook contains the following entries:\n")
pprint.pprint(phoneBook)
You can use format() to make your life easy:
for i in phoneBook.iteritems():
print("{0} {1}".format(*i))
my_dictionary = {}
while True:
name = str(input("Enter a name: "))
if name == "":
break
elif name in my_dictionary:
print "Phone Number: " + my_dictionary[name]
else:
phone_number = input("Enter this person's number: ")
my_dictionary[name] = phone_number
print my_dictionary

Categories

Resources