House Point Program - python

My name is Jamie, I'm a Yr 12 student currently living in NZ. At school, in our computer science class we were tasked with creating a program for house points. A house is sort of like the Houses in Harry Potter, each student is assigned to one. These houses then compete in events and earn points, at the end of the year the house with the most points wins the trophy.
Now we have only been taught about 2-D arrays and parallel lists, as these need to be incorporated plus it must be modular.
The program must be fully user inputed as requirements for excellence (equivalent of A) must be user inputed.
The program must also have these inputs and outputs:
Inputs: House Names, House Events, and Points earned in events for the house
Cancel house name and house event entry when XXX is entered.
Outputs: Winner of each event, house with best average, house with most wins, and overall winner.
I am currently trying to figure out how to do the points to go with house and events.
Appreciate all help,
Jamie :)
EDIT: Posted Code
def number_house():
global numhouse
print("Welcome!")
print()
Flag = True#loop
while Flag:
try:
numhouse = int(input("Please enter the number of events there is: "))
print()
if numhouse < 1 or numhouse > 100:
print("WOW, thats a lot of events, please be reasonable. Thanks.")
else:
Flag = False
except ValueError:
print("Enter only a number, Thanks.")
def event_names():
global event
print("Enter XXX when finished entering event names")
Flag = True
for e in range(numhouse):
e = input("Event name: ")
if e == 'XXX' or e == 'xxx':
Flag = False
else:
event.append(e)
print()
def getData():
global data
global inputlist
global event
lower_bound = 0
upper_bound = 100
k=0
n=str(input("Please enter house names <<<Enter XXX when finished>>> :"))
while n != 'XXX' :
if n == 'XXX' or n == 'xxx':
exit
data = [n]
print()
print ("Please enter the event points in ascending order. ",event,"Thanks")
for k in range(len(event)):
s = getScore(n,lower_bound,upper_bound)
data=data+[s]
inputlist = inputlist + [data]
n=str(input("Please enter house names <<<Enter XXX when finished>>> :"))
def getScore(name,min,max):
global event
sc= -1
while sc < min or sc > max :
try :
sc = int(input("Please enter score for "+ name + " :"))
except ValueError :
print("Invalid Input please enter an interger. Thanks")
return sc
score =[]
getscore = []
data = []
inputlist = []
event = []
number_house()
event_names()
getData()
print()
print(inputlist)

LOWER_BOUND = 0
UPPER_BOUND = 100 # both in caps because this is a constant
def get_score(house_name, event_name):
# an extra argument so you can tell which event is being scored.
# removed the min, max cause we are using the constants!
score = -1
while score < LOWER_BOUND or score > UPPER_BOUND:
try:
score = int(input("Please enter score for %s in the event %s:" % (house_name, event_name)))
if score < LOWER_BOUND :
print ("Score is too low, minimum score is %i.\nPlease try again." % min_score)
if score > UPPER_BOUND:
print ("Score is too high, maximum score is %i\nPlease try again." % max_score)
except ValueError:
print("Invalid Input please enter an integer. Thanks")
return score # note the use of return to avoid using global
def get_number_of_events():
print("Please enter the number of events there is.")
while True:
try:
n_events = int(input(">>"))
except ValueError:
print("Enter only a number, Thanks.")
if n_events > 100:
print("WOW, that's a lot of events, please be reasonable. Thanks.")
elif n_events < 1:
# this is a different condition that would get a wrong error in your program,
# note the use of 'elif', in Python this an 'else if'.
print ("That's too few events! Try Again.")
else:
# no need to use Flag, just use break when you want to leave a loop.
break
return n_events
def get_events_names(n_events):
print ("Please enter the events names")
events = []
for n in range(1, n_events + 1):
# starting at 1 to give a better display
event_name = input("Event %i name: " % n)
events.append(event_name)
return events
def get_data(events):
data = []
while True:
house_name = input("Please enter house names <<<Enter XXX when finished>>> :")
if house_name.upper() == "XXX":
# using .upper() to avoid checking twice for either 'xxx' or 'XXX'.
# I would ask the user for how many houses are there instead, but your choice ;)
break
print ("Please enter the events points in ascending order.")
# not actually need to be in ascending order, you can sort them later if you want.
scores = []
for event_name in events:
# don't use range(len(something)), loops in Python are easy!
score = get_score(house_name, event_name)
scores.append([event_name, score])
data.append([house_name, scores])
# use .append() instead of data = data + [...]
return data
def main():
print("Welcome!\n")
n_events = get_number_of_events()
events_names = get_events_names(n_events)
print()
data = get_data(events_names)
print()
for house_name, event_data in data:
print ("House " + house_name)
for event_name, score in event_data:
# note the use of tuple unpacking
print ("\tEvent: %s Score: %i" % (event_name, score))
if __name__ == '__main__':
main()
This time maintaining the same structure as your program.
Check comments for some tips and tricks.
Also, try to keep your variable names with meaning and check the PEP 8 guidelines for naming conventions (variables and functions should be snake_case,
Output:
Welcome!
Please enter the number of events there is.
>>2
Please enter the events names
Event 1 name: Quidditch Match
Event 2 name: Duels
Please enter house names <<<Enter XXX when finished>>> :Gryffindor
Please enter the events points in ascending order.
Please enter score for Gryffindor in the event Quidditch Match:100
Please enter score for Gryffindor in the event Duels:30
Please enter house names <<<Enter XXX when finished>>> :Slytherin
Please enter the events points in ascending order.
Please enter score for Slytherin in the event Quidditch Match:40
Please enter score for Slytherin in the event Duels:50
Please enter house names <<<Enter XXX when finished>>> :XXX
House Gryffindor
Event: Quidditch Match Score: 100
Event: Duels Score: 30
House Slytherin
Event: Quidditch Match Score: 40
Event: Duels Score: 50

I'll try to give you a different approach, see if that helps you.
def main():
events = []
houses = []
scores = {} # key = House name, value = scores
print "Welcome!\n"
# create the houses
print "Please enter how many houses there is:"
n_houses = int(raw_input(">>"))
print "Please enter houses names:"
for n in range(n_houses):
print "House", n+1
house_name = raw_input(">>")
houses.append(house_name)
# create the events
print "Please enter the number of events there is"
n_events = int(raw_input(">>"))
print "Please enter the event names"
for n in range(n_events):
print "Event", n+1
event_name = raw_input(">>")
events.append(event_name)
# get the scores for each house for each event
for event in events:
for house in houses:
print "Please enter the score for House %s in the event %s"%(house, event)
score = int(raw_input(">>"))
# initialize the score with a empty list
if house not in scores:
scores[house] = []
# add the score list
scores[house].append(score)
print "\nThe result is:"
# process the result
for house, score in sorted(scores.items(),
key=lambda x: sum(x[1]),
reverse=True):
print "House %s. Total Score: %i"%(house, sum(score))
if __name__ == "__main__":
main()
First thing you should notice is that I'm not using global, using global is usually frowned upon, it can lead to undesired interactions on data.
Also, instead of asking for inputs like "XXX" to break the loop, I asked the user for the number of inputs he wants to deal before, so I can loop over this number and process each separately.
I do the same thing with the house, I ask for how many houses are there, and then their names.
Next I do a nested for loop with the event names and house names. The order matters, we deal with each event first. You can change it to deal with each house first.
And finally I process the scores. the line for house, score in sorted(scores.items(), key=lambda x: sum(x[1]), reverse=True): is a bit clogged and advanced but it means this: I want to loop over a sorted list of items, giving me two items at a time, the items are named house and score, they will be sorted by the function sum(x[1]), and I want this in the reversed order (or else the last would show in first).
key=lambda x: sum(x[1]) is a bit of a hack, it could be done better. lambda means a function, it takes x as input, x in this case is a tuple of house, score, so I want the score, so I access it using x[1], and since I want the sum I use sum(x[1]).
Usage:
Welcome!
Please enter how many houses there is:
>>2
Please enter houses names:
House 1
>>Gryffindor
House 2
>>Slytherin
Please enter the number of events there is
>>2
Please enter the event names
Event 1
>>Quidditch Match
Event 2
>>Duels
Please enter the score for House Gryffindor in the event Quidditch Match
>>100
Please enter the score for House Slytherin in the event Quidditch Match
>>90
Please enter the score for House Gryffindor in the event Duels
>>250
Please enter the score for House Slytherin in the event Duels
>>240
The result is:
House Gryffindor. Total Score: 350
House Slytherin. Total Score: 330
Notice that this was made on Python 2.7, to port to Python 3 just change raw_input to input and print to print()

Related

Extracting a tuple value from a dictionary in python

am working on a restaurant project, and I need a hand :)
so the program will display the main dishes, and the user has to enter which dish he/she wants..
and then the choice will be taken and then added to the bill (dish name, price, num of items)...
so far I chose a dictionary so when the user enters 1 (key), the program will display Mashroum Risto...
this what've created:
dishes = {1 : ('Mashroum Risito', 3.950), 2 : ['Tomato Pasta', 2.250],3:['Spagehtie',4.850]}
now my question is how to get the dish name without the price (the price is 3.950) and extract it! and also how to get the price without the name so then I can send it into the bill function to calculate it? and if you have any suggestions please go ahead, because i don't know if using dictionary was the right choice
def MainDish():
dishes = {1 : ('Mashroum Risito', 3.950), 2 : ['Tomato Pasta', 2.250],3:
['Spagehtie',4.850]}
dishes.values
print("1. Mashroum Risito 3.950KD")
print("2. Tomato Pasta 2.250KD")
print("3. Spagehtie 4.850KD")
choice = eval(input('Enter your choice: '))
NumOfItem = eval(input('How many dish(es): '))
while(choice != 0):
print(dishes.get(choice)) #to display the item only without the
price
a = dishes.values()
recipient(a)
break
The way you have implemented it:
print (dishes[1][0]) #will give you name
print (dishes[1][1]) #will give you price
where [x][y]
x = key in the dict (input in your case)
y = element of the value in the dict (0 = name, 1=price in your case)
You should probably create the dictionary better as below:
Follow up question is not so clear but I think this is what you are after roughly. You will need to tweak the returns to your usage:
def MainDish():
NumOfItem = float(input('How many dish(es): '))
dish = list(dishes)[choice-1]
cost_of_dish = dishes[dish]
totalcost = cost_of_dish * NumOfItem
print (f"\n\tOrdered {NumOfItem}x {dish} at {cost_of_dish}KD each. Total = {totalcost}KD\n")
return dish, cost_of_dish, totalcost
dishes = {'Mashroum Risito': 3.950, 'Tomato Pasta': 2.250}
for key,val in dishes.items():
print (f"{list(dishes).index(key)+1}. {key}: {val}KD")
keepgoing = True
while keepgoing:
choice = int(input('Enter your choice: '))
if choice == 0:
keepgoing = False
break
else:
dish, cost_of_dish, totalcost = MainDish()

How can you multiply a value from a dictionary with a number to get a real value?

store={'Rice':450,'Beans':200,'Egg':40,'Fish':250,'Spag':250}
bill=()
total=()
print('Welcome!!!we sell:',"\n",[store])
while True:
a=input('What would you like to buy?=')
b=input('how many of each product do you want?=')
if a in store:
bill=store[a]*b
print('bill=',bill)
elif a not in store:
print('Sorry we don\'t have that')
else:
total=bill+total
print('Total=',total)
Your if/elif/else is impossible, because wether a is IN the dict, wether it isn't, there is no way you into the else so put it in the if.
I'd also added a way to stop the loop by entering stop without you won't be able to stop the program and print the final total
move b input into the if, that isn't necessary to ask for quantity if the product isn't available
added int() conversion on b
store = {'Rice': 450, 'Beans': 200, 'Egg': 40, 'Fish': 250, 'Spag': 250}
print('Welcome!!!we sell:', "\n", store)
total, bill = 0, 0
while True:
a = input('What would you like to buy? ("stop" to quit): ')
print(">" + a + "<") # For OP debug purpose only, to be removed
if a == "stop":
break
if a in store:
b = int(input('how many of each product do you want?='))
bill = store[a] * b
total += bill
print(f"Total={total} (+{bill})")
else:
print('Sorry we don\'t have that')
print('Total=', total)

Order generator program

I am trying to make a short program which will take user input, append the input to a list and then randomly determine the order of the elements in the list.
this is what I thought out:
from random import choice
participants = []
prompt = "\nPlease enter players first name: "
prompt += "\nPlease enter 'q' when all players have been entered: "
while True:
user = input(prompt)
if user == 'q':
break
print(f"There are {slot} participants")
else:
participants.append(user)
slot = len(participants)
print("\n\n")
for participant in participants:
print(f"{participant}")
print(f"So far, there are {slot} participants")
while participants:
for participant in participants:
person_of_choice = choice(participants)
person_in_question = participants.remove(person_of_choice)
print(person_in_question)
However, I am getting the following output
Mark Stark Dark So far, there are 3 participants
Please enter players first name: Please enter 'q' when all players
have been entered: q None None None
How do I change the ordering from none into their names?
I've changed a few things in the code, and the changes are commented out with ##.
you can try it to see the output.
from random import choice
participants = []
prompt = "\nPlease enter players first name: "
prompt += "\nPlease enter 'q' when all players have been entered: "
# init slot to zero
slot = 0
while True:
user = input(prompt)
if user == 'q':
print(f"There are {slot} participants")
## if you want to use `print` function before `break`,
## you must put the `break` statement under the `print` function
break
else:
participants.append(user)
slot = len(participants)
print("\n\n")
for participant in participants:
print(f"{participant}")
print(f"So far, there are {slot} participants")
while participants:
for participant in participants:
person_of_choice = choice(participants)
print(person_of_choice)
# person_in_question = participants.remove(person_of_choice)
## person_in_question is None, because `remove` always return None
## you can this from here https://docs.python.org/3/tutorial/datastructures.html
participants.remove(person_of_choice)

comparing user input to a dictionary in python

I have to create a program that lets a user input what courses they have taken(one at a time), and compare it to a dictionary of "courses" with the pre-requisites and print what courses that student is eligible to take. I am not sure on how to compare the user input to the dictionary to print what courses they can take. Here is what I have so far
print "Enter a course(0 to quit): "
courses = raw_input()
d = {150:[150],
161:[161],
162:[161],
231:[162],
241:[161],
251:[251],
260:[150],
300:[241],
303:[162],
304:[162],
307:[162],
353:[231],
385:[353],
355:[231],
461:[303,304,307,231],
475:[303,304,307],
480:[470]
}
while courses =! '':
if courses in d.keys():
print("You have taken: ", courses)
if courses == 0:
break
You are only getting input once. You need to get input in a loop:
d = {150:[150],161:[161],162:[161],231:[162],241:[161],251:[251],260:[150],300:[241],303:[162],304:[162],307:[162],353:[231],385:[353],355:[231],461:[303,304,307,231],475:[303,304,307],480:[470]}
prereqs = set()
while True:
course = int(raw_input("Enter a course you have taken (0 to quit): "))
if course == 0:
break
try:
prereqs.update(d[course])
except KeyError:
print '\t\t\t\t\tHmm...I don\'t know that course'
In the while loop, we are getting input every iteration. If it is 0, we break out of the loop. If not, we try to lookup the course in the dict. If this fails, we print the "error" message. You should be able to take it from here(prereqs stores the courses that you have took in a set).

Sentinel values with while loop

The program I'm writing is to use a given formula to calculate financial assistance for families that loops back and asks the user if they want to do it for another family. Part of the instructs was also to use -1 as a sentinel value for the input. Here is what I have at the moment:
def main():
cont = "y"
while cont.lower() == "y":
try:
income = float(input("Please enter the annual household income:"))
children = int(input("How many children live in the house?"))
amountAssistance = assistanceTotal(income, children)
print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
cont = input("Would you like to run this again? (Y/N)")
except:
print("Something went wrong.")
print("Did you enter a whole number of children?")
cont = input("Would you like to try again? (Y/N)")
def assistanceTotal(money, kids):
if (money >= 30000 and money <= 40000) and kids >= 3:
moneyTotal = 1000 * kids
return moneyTotal
if (money >= 20000 and money <= 30000) and kids >= 2:
moneyTotal = 1500 * kids
return moneyTotal
if money < 20000:
moneyTotal = 2000 * kids
return moneyTotal
main()
I've tried looking up info on using sentinel values with loops like this but I haven't been able to quite grasp if how I'm thinking of using it is correct. For this type of a situation would I need one sentinel value or two, one for income and one for children? I think I would also need to alter my cont statements and change how it reacts if they type in a -1. Any help is appreciated even if it is just directing me to accurate info on how to use sentinels.
Edit to add another sort of follow up question:
Do sentinel values have to be numbers or are they most commonly numbers? What is the difference between having a continue portion using yes/no inputs to using sentinel values? For an example: In my text book it shows using a sentinel to stop the loop by asking for either an input of an integer or enter -1 to exit, is this not the same thing as asking continue Y/N?
If you want all of the inputs to be able to detect the sentinel value, you can use a custom input function that raises a RuntimeError (or a custom exception of your own) and handle the exception in a uniform way.
def main():
def get_input(prompt):
i = input(prompt)
if i == '-1':
raise RuntimeError()
return i
cont = "y"
while cont.lower() == "y":
try:
income = float(get_input("Please enter the annual household income:"))
children = int(get_input("How many children live in the house?"))
amountAssistance = assistanceTotal(income, children)
print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
cont = input("Would you like to run this again? (Y/N)")
except RuntimeError:
print("Done.")
break
except:
print("Something went wrong.")
print("Did you enter a whole number of children?")
cont = input("Would you like to try again? (Y/N)")
Generally you would use the sentinel to exit the loop, in this case entering -1 for income, e.g.:
while True:
try:
income = int(input("Please enter the annual household income (-1 to exit):"))
if income == -1:
break
children = int(input("How many children live in the house?"))
amountAssistance = assistanceTotal(income, children)
print("For a household making", income, "per year with", children, " children the amount of assistance would be", amountAssistance)
except ValueError:
print("Something went wrong.")
print("Did you enter a whole number of children?")
NB 1: given the sentinel is -1 and all of the tests for income are against ints I would suggest keeping income an int.
NB 2: it is best to catch explicit exceptions, e.g. ValueError vs a catch-all except: clause

Categories

Resources