Anonymous - 1 year ago
406 0

No description

Python

# Pre_release

``````no_students = 3 #SELECT HOW LONG YOU WANT YOUR ARRAYS TO BE!!!

#Declaration of all Arrays
age = [None] * no_students
school_house = [None] * no_students
reaction_time = [None] * no_students

#Initialization of loop which will run for the ammount of students there are. Since it starts from 0 there is no need to make it no_students+1
for i in range(0,no_students):

stu_age_check = False #Boolean to check if correct input is entered.
while (stu_age_check == False): #The loop will only run if an invalid input is detected otherwise it will stop.
stu_age = 0
stu_age = int(input("Enter student age. It should be between 12 and 16 inclusive. The age should be in number: "))
if (stu_age < 12 or stu_age > 16 or stu_age == None): #Types of input that will be rejected.
print("Please enter a valid age within the limits.") #Prompt to enter correct input.
stu_age_check = False  #This line isn't really needed.
else:
stu_age_check = True #This is necesarry to stop the loop.
age[i] = stu_age #This adds the correct value to the respective index number in the array.
print("") #Just for presentation's sake.

house_choice = None #Not needed but doesn't hurt :P
correct_house = False #Boolean to check if correct input is entered.
while (correct_house == False):
print("Enter 0 if your house is Saturn and 1 if your house is Mars")
print("0. Saturn")
print("1. Mars")
house_choice = int(input("Enter student house [0-1]: "))
if (house_choice == 0):                                           #Since there are only 2 possible inputs here,
school_house[i] = "Saturn"                                    #it is possible to use a 0 and a 1 to create a list to select the house,
correct_house = True                                          #this makes it easier for the user
elif (house_choice == 1):                                         #while making the code sleeker and less error-prone.
school_house[i] = "Mars"
correct_house = True
else:
print("Please input only 0 or 1 as input for students's house")
print("") #Just for presentation's sake.

reaction_time_check = False #Boolean to check if correct input is entered.
while (reaction_time_check == False):
stu_reaction_time = float(input("Input reaction time in seconds: "))
if (stu_reaction_time < 0 or stu_reaction_time > 10 or stu_reaction_time == None): #Types of input that will be rejected.
print("Please re-enter correct reaction time in seconds. It must be in numbers with no units.") #Prompt to enter correct input.
reaction_time_check = False #This line isn't really needed
else:
reaction_time_check = True #Exists loop.
reaction_time[i] = stu_reaction_time
print("") #Just for presentation's sake.

#print (age, school_house, reaction_time) #Error checking!!!

student_number = 0
saturn_total = 0
saturn_students = 0                                 #Issue with this is too many variables are needed.
mars_total = 0
mars_students = 0
for x in reaction_time:
if (school_house[student_number] == "Saturn"):
saturn_total += x
saturn_students += 1
else:
mars_total += x
mars_students += 1
student_number += 1
print("The average reaction time for students in Saturn is: ", saturn_total/saturn_students)
print("The average reaction time for students in Mars is: ", mars_total/mars_students)

#Average and slowest reaction time of student in particular age and house
total_time = 0
avg_time = 0
count = 0
count2 = 0
slowest_time = 9999
selected_house = input("Enter selected house Mars or Saturn ") #User selects desired house
selected_age = int(input("Enter selected age")) #User selects desired age
for x in reaction_time: #Loop runs over all the reation times
if (school_house[count] == selected_house and age[count] == selected_age): #This limits the results of reaction_time[] to those students only in selected_house AND selected_age
total_time += x #Total time for students in selected_house and selected_age
count2 += 1 #count for students in selected_house and selected_age
if (x < slowest_time): #This makes sure the current value of the array reaction_time is lower than the previously lowest stored reaction time in the variable slowest_time
slowest_time = x #If yes, that is now the new slowest reaction time
count += 1
final_average = total_time/count2 #Calculates the average
print("The average reaction time for students aged:", selected_age, " and in house:", selected_house, " is:", total_time/count2)
print("The slowest reaction time for students aged:", selected_age, " and in house:", selected_house, " is:", slowest_time)``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download