loop questions Python - 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

Related

Dictionary phonebook with userInput, but with a twist

I have been working on this for too long. It should be simple and I've ran through many different combinations, however, I keep getting the code wrong and have no idea why. It works fine when I have manual input, but when I submit there is an error.
question prompt:
Write a program that keeps a dictionary of names and their corresponding phone numbers.
Repeatedly ask the user for a name. Then, do one of the following three things, depending on what they enter:
If they enter nothing, exit the program.
If they enter a name that exists as a key in your dictionary, simply print the corresponding phone number.
If they enter a name that is NOT in your dictionary as a key, ask the user for a phone number, and then put the name and phone number in your dictionary.
Print out the final dictionary.
my code:
phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
if not name in phoneBook:
number = input("Please enter number: ")
print "Phone number: " + number
phoneBook[name] = number
name = input("Please enter a name(or press enter to end input): ")
if name in phoneBook:
print phoneBook[name]
if name == '':
break
print phoneBook
Expected result:
Phone number: 1234
Phone number: 5678
{'Tracy': '5678', 'Karel': '1234', 'Steve': '9999'}
My result:
Phone number: 1234
Phone number: 5678
Phone number: 9999
1234
Phone number: 9999
5678
Phone number: 9999
{'Tracy': '9999', 'Karel': '9999', 'Steve': '9999'}
you must also access the dictionary keys for checking the existence of a name:
if not name in phoneBook.keys():
phoneBook = {}
name = input("Please enter a name(or press enter to end input): ")
while name != '':
if not name in phoneBook.keys():
number = input("Please enter number: ")
phoneBook[name] = number
print "Phone number: " + number
name = input("Please enter a name(or press enter to end input): ")
else:
print phoneBook[name]
print phoneBook
Try the above code.
When a name is not in the phoneBook, then assign the name a number, so phoneBook[name] = number should be in the if not name in phoneBook.keys(): block. And then enter another name in the same if block.

How to make while True loop running in python?

I want to write a program to have the (name, age, height) where name is string, age and height are numbers for at least 2 users. So I have tried to use a while True loop but it breaks after just one entry (name, age, height). This is because number of items in the list is tree. How could I make a tuple so that the number of items would count as one for all the name, age and height? or is there any easy way?
data=[]
while True:
name = raw_input("Please enter your name: ")
age = int(raw_input("Please enter your age: "))
height = int(raw_input("Please enter your height: "))
data.append(name)
data.append(age)
data.append(height)
if len(data) <2:
print "you need to enter at least 2 users"
else:
break
print data
Try
data=[]
while len(data) < 2:
name = raw_input("Please enter your name: ")
age = int(raw_input("Please enter your age: "))
height = int(raw_input("Please enter your height: "))
data.append({
'name': name,
'age': age,
'height': height,
})
print data
It is because you put name instead of dict of user information (name, age, height).
You can use range
Ex:
data=[]
for _ in range(2):
name = raw_input("Please enter your name: ")
age = int(raw_input("Please enter your age: "))
height = int(raw_input("Please enter your height: "))
data.append((name, age, height))
print(data)
Or: using a while loop.
data=[]
while True:
name = raw_input("Please enter your name: ")
age = int(raw_input("Please enter your age: "))
height = int(raw_input("Please enter your height: "))
data.append((name, age, height))
if len(data) == 2:
break
print(data)

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 passing list to class and attempting to reference values at index

I am attempting to create a list with 5 students information listed, which is then passed to the Student class. It seems that I have been successful in doing this, but now I am unable to figure out how to access and modify any of the previously added values with the list.
class Student:
def __init__(self, student_name, student_id, student_gpa, student_grade, student_time):
self.student_name = student_name
self.student_id = student_id
self.student_gpa = student_gpa
self.student_grade = student_grade
self.student_time = student_time
student_list = []
for i in range(5):
student_name = input("Please enter student's name: ")
student_id = input("Please enter student's ID number: ")
student_gpa = input("Please enter student's GPA: ")
student_grade = input("Please enter student's expected grade: ")
student_time = input("Please enter if student is a part or full time student: ")
student_list.append(Student(student_name, student_id, student_gpa, student_grade, student_time))
How might I add the option to reference a Student's name and alter the grade from what was originally entered?
Here you have a list of Student objects and the list is pretty small.So,you can loop through the list and find the student.
Here,the student id is an unique way of identifying the particular student.
def change_gpa(id):
for student in student_list:
if student.student_id == id:
# Take input from the user of the changes to be made
# Example gpa
student.student_gpa = input('Enter new gpa')
return
change_gpa(input('Enter the student id whose credentials have to be changed'))
You ahve a list containing instances of the Student class, you can simply iterate over it, for example:
for student in student_list:
if student.student_id == something:
student.student_grade = whatever #it changes the value
or, if you know exactly which student you want to modify in the list you can do:
student_list[0].student_grade = whatever #assuming you want to modify the first student in the list
Python 3:
class Student:
def __init__(self, student_name, student_id, student_gpa, student_grade, student_time):
self.student_name = student_name
self.student_id = student_id
self.student_gpa = student_gpa
self.student_grade = student_grade
self.student_time = student_time
student_list = []
for i in range(3):
student_name = input("Please enter student's name: ")
student_id = input("Please enter student's ID number: ")
student_gpa = 3.73
student_grade = "B-"
student_time = 2017
'''
student_gpa = input("Please enter student's GPA: ")
student_grade = input("Please enter student's expected grade: ")
student_time = input("Please enter if student is a part or full time student: ")
'''
student_list.append(Student(student_name, student_id, student_gpa, student_grade, student_time))
print("Origianl Information")
for student in student_list:
print(student.student_id,student.student_name,student.student_gpa)
search_student = input("Enter a student name to change GPA: ")
for student in student_list:
if student.student_name == search_student:
student_gpa = input("Enter new GPA of "+search_student+": ")
student.student_gpa = student_gpa
print("Updated Information")
for student in student_list:
print(student.student_id,student.student_name,student.student_gpa)
Output:
Please enter student's name: Shovon
Please enter student's ID number: 2389
Please enter student's name: Aslam
Please enter student's ID number: 2383
Please enter student's name: Nafis
Please enter student's ID number: 56
Origianl Information
2389 Shovon 3.73
2383 Aslam 3.73
56 Nafis 3.73
Enter a student name to change GPA: Shovon
Enter new GPA of Shovon: 3.96
Updated Information
2389 Shovon 3.96
2383 Aslam 3.73
56 Nafis 3.73
N.B.: I have commented some of the lines for reducing the huge input. Uncomment those.

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

Categories

Resources