Removing Excessive Loops - python

This is a very basic program. It stores the student data and then displays the student name with the highest marks.
Ignoring the time complexity for my code, how can I format my code in such a way that it uses less while loops.
student_names, test_1, test_2, test_3, total_score = [], [], [], [], []
i = 0
while i < 30:
student = input("Student: ").title()
student_names.append(student)
while True:
test1 = int(input("Test-1 score: "))
if 0 <= test1 <= 20:
test_1.append(test1)
break
else:
print("Range Error for Test1 Score :( ")
while True:
test2 = int(input("Test-2 score: "))
if 0 <= test2 <= 25:
test_2.append(test2)
break
else:
print("Range Error for Test2 Score :( ")
while True:
test3 = int(input("Test-3 score: "))
if 0 <= test3 <= 35:
test_3.append(test3)
print('')
break
else:
print("Range Error for Test3 Score :( ")
total = test_1[i] + test_2[i] + test_3[i]
total_score.append(total)
max_score = total_score[0]
if total_score[i] > max_score:
max_score = total_score[i]
i += 1
avg_score = 0
for i in range(30):
avg_score += total_score[i]
print(f"Average Score: {avg_score/3}")
print(f"{student_names[total_score.index(max_score)]} scored Highest of {max_score}/{35+25+20}")

You can factor the duplicated input validation code.
def input_int(name, max_score):
while True:
x = int(input(name))
if 0 <= x <= max_score:
return x
else:
print('Range error for %s :(' % name)
and then in your code:
test_1.append(input_int('Test 1 Score', 20))
test_2.append(input_int('Test 2 Score', 25))
test_3.append(input_int('Test 3 Score', 35))

Related

How to better implement a history of user action?

I decided to make a calculator as a project.
Implementing basic addition, subtraction, division, and multiplication was fairly easy.
I wanted to add more functionality so I decided to implement a list of results the user view. However, I had a difficult time keeping track of the results numerically. I wrote a maze of if statements that are functional but seem to be overwrought with code. I am sure there is a better way to handle this.
Any advice?
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
def div(x, y):
value = None
while True:
try:
value = x / y
break
except ZeroDivisionError:
print('Value is not dividable by 0, try again')
break
return value
def num_input(prompt='Enter a number: '):
while True:
try:
print(prompt, end='')
x = int(input())
break
except ValueError:
print('You must input a number. Try again.')
return x
def get_two_val():
x, y = num_input(), num_input()
return x, y
print("Welcome to Simple Calc")
# declaration of variables
num_of_calc_counter = 0
index_of_calc = 1
calculations = []
while True:
print("Choose from the following options:")
print(" 1. Add")
print(" 2. Subtract")
print(" 3. Multiply")
print(" 4. Divide")
print(" 5. Sales Tax Calculator")
print(" 6. Recent Calculations")
print(" 0. Quit")
usrChoice = num_input('Enter your choice: ')
'''
Menu workflow
options 1-4 take in two numbers and perform the specified calculation and
then add the result to a master list that the user can reference later.
lastly, the workflow increments the num_of_calc variable by 1 for recent
calc logic
option 5 is a simple tax calculator that needs work or option to enter
or find tax rate
option 6 returns a list of all the calculations perform by the user
'''
if usrChoice is 1:
numbers = get_two_val()
result = add(*numbers)
print(numbers[0], "plus", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 2:
numbers = get_two_val()
result = sub(*numbers)
print(numbers[0], "minus", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 3:
numbers = get_two_val()
result = mul(*numbers)
print(numbers[0], "times", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 4:
numbers = get_two_val()
result = div(*numbers)
print(numbers[0], "divided by", numbers[1], "equals", result)
calculations.extend([result])
num_of_calc_counter += 1
elif usrChoice is 5:
tax_rate = .0875
price = float(input("What is the price?: "))
total_tax = tax_rate * price
final_amount = total_tax + price
print('Tax rate: ', tax_rate, '%')
print('Sales tax: $', total_tax)
print('_____________________________')
print('Final amount: $', final_amount)
#
elif usrChoice is 6:
if len(calculations) is 0:
print('There are no calculations')
elif num_of_calc_counter == 0:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif index_of_calc == num_of_calc_counter:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif num_of_calc_counter > index_of_calc:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter -= 1
elif num_of_calc_counter < index_of_calc:
index_of_calc = 1
for i in calculations:
print(index_of_calc, i)
index_of_calc += 1
num_of_calc_counter += 1
elif usrChoice is 0:
break
I don't know if you could find this simpler:
def num_input(prompt='Enter a number: '):
finished = False
while not finished:
string_input = input(prompt)
try:
input_translated = int(string_input)
except ValueError:
print('You must input a number. Try again.')
else:
finished = True
return input_translated
def division_operation(x, y):
if y == 0:
print('Value is not dividable by 0, try again')
return None
else:
return x / y
math_operations_values = [
(lambda x, y: x + y, 'plus'),
(lambda x, y: x - y, 'minus'),
(lambda x, y: x * y, 'times'),
(division_operation, 'divided by')
]
def get_two_val():
return (num_input(), num_input())
def operate_on_numbers(operation_index):
def operate():
numbers = get_two_val()
operator, operation_string = math_operations_values[operation_index]
result = operator(*numbers)
if result is not None:
print(numbers[0], operation_string, numbers[1], "equals", result)
calculations.append(result)
return operate
def tax_computation():
tax_rate = .0875
price = float(input("What is the price?: "))
total_tax = tax_rate * price
final_amount = total_tax + price
print('Tax rate: ', tax_rate * 100, '%')
print('Sales tax: $', total_tax)
print('_____________________________')
print('Final amount: $', final_amount)
def show_computations():
if calculations:
for (index, values) in enumerate(calculations, start=1):
print(f'{index}: {values}')
else:
print('There are no calculations')
calculations = []
finished = False
choices_actions = [
operate_on_numbers(0),
operate_on_numbers(1),
operate_on_numbers(2),
operate_on_numbers(3),
tax_computation,
show_computations
]
while not finished:
print("""
Choose from the following options:
1. Add
2. Subtract
3. Multiply
4. Divide
5. Sales Tax Calculator
6. Recent Calculations
0. Quit""")
user_choice = num_input('Enter your choice: ')
'''
Menu workflow
options 1-4 take in two numbers and perform the specified calculation and
then add the result to a master list that the user can reference later.
lastly, the workflow increments the num_of_calc variable by 1 for recent
calc logic
option 5 is a simple tax calculator that needs work or option to enter
or find tax rate
option 6 returns a list of all the calculations perform by the user
'''
if user_choice == 0:
finished = True
else:
try:
operation_to_do = choices_actions[user_choice - 1]
except IndexError:
print('Please enter one of choice shown.')
else:
operation_to_do()

Python3 - while ids > Stop: TypeError: unorderable types: str()> int()

Hello I am trying to run a program that will return back students grades and their average. ALSO I KNOW I AM A BASIC BRAINLESS FIRST YEAR PROGRAMMER. I WILL PROBABLY BE HORRIBLE AT PROBABLY. HOWEVER PLEASE HELP THE BEST YOU CAN IT WOULD BE GREATLY APPRECIATED.
THANK YOU.
The error says to be in line 49.
saying that"
line 49, in <module>
while ids > STOP:
TypeErrorL unorderable types: str() > int()
XXXX
def assigngrades(scores):
avg = sum(scores)/len(scores)
print(avg)
for val in scores:
if val > avg + 10:
grade = 'A'
elif val > avg + 5:
grade = 'B'
elif val > avg -5:
grade = 'C'
elif val > avg - 10:
grade = 'D'
else:
grade = 'F'
grades.append(grade)
print("in assigngrades, grades: ",grades)
return grades
def printsummary(grades, ave):
print('ID Score Average Grade')
print('===========================================')
print( )
for val in range(len(ids)):
print('val', val)
print(ids,' ',scores, ' ', grades)
return
#main
ids = []
scores = []
grades = []
STOP = 0
ids = input("Enter an ID:")
while ids > STOP:
ids.append(ids)
score = eval(input("Enter a score:"))
scores.append(score)
id = (input("Enter an ID number, 0 to STOP:"))
grades = assigngrades(scores)
print("after while loop")
print("Ids:", ids, "Scores:", scores, "Grades:", grades)
printsummary(grades, avg)
Nonetheless, I am confused on what is the issue.I appreciate your time and help looking at this. Thank you so so so much. Yes I know I am stupid.
The input() function returns a string, so you should convert it to an integer with int() so you can compare its value with another integer. You should also name the variable that stores the user input something other than ids since you already define it as a list:
ids = []
scores = []
grades = []
STOP = 0
id = int(input("Enter an ID:"))
while id > STOP:
ids.append(id)
score = eval(input("Enter a score:"))
scores.append(score)
grades = assigngrades(scores)
print("after while loop")
print("Ids:", ids, "Scores:", scores, "Grades:", grades)
printsummary(grades, avg)

Infinite loop for python program

I have a problem with this code, this program should keep allowing you enter students until the number of passes reaches 8 or the total number of students reaches 10. However currently it just keeps asking for input and hence there is an infinite loop. How do I go about fixing this?
total_students=0
student_passes=0
student_failures=0
while (total_students <= 10) or (student_passes != 8):
result=int(input("Input the exam result: "))
if result>=50:
student_passes = student_passes + 1
else:
student_failures = student_failures + 1
total_students = total_students + 1
print (student_passes)
print (student_failures)
if student_passes >= 8:
print ("Well done")
Change or to and. While both are true you continue:
total_students=0
student_passes=0
student_failures=0
while (total_students != 10) and (student_passes != 8): # != or <
result=int(input("Input the exam result: "))
if result>=50:
student_passes += 1
else:
student_failures += 1
total_students +=1
print (student_passes)
print (student_failures)
you might have to revisit your code. I m not python expert, however I believe you should modify the condition for while loop.
such as while (total_students <= 10) or (student_passes <= 8):
this will resolve your problem.
total_students=0
student_passes=0
student_failures=0
while (total_students <= 10) or (student_passes <= 8):
result=int(input("Input the exam result: "))
if result>=50:
student_passes = student_passes + 1
else:
student_failures = student_failures + 1
total_students = total_students + 1
print (student_passes)
print (student_failures)
if student_passes >= 8:
print ("Well done")
You should use and instead of or to meet your requirement.
total_students=0
student_passes=0
student_failures=0
while (total_students <= 10 and student_passes < 8):
result=int(input("Input the exam result: "))
if result>=50:
student_passes = student_passes + 1
else:
student_failures = student_failures + 1
total_students = total_students + 1
print (student_passes)
print (student_failures)
if student_passes >= 8:
print ("Well done")

Why can't 'tm' be registered as a variable out side the if statement?

run = 1
list1 = []
for i in range(2):
while run > 0 :
print("-------------------------------------------------------")
run = 1
reg_num = input("Registration Number in caps: ")
tsh = int(input("Hour Entered : "))
tsm = int(input("Minute Entered : "))
tss = int(input("Second Entered : "))
print("")
teh = int(input("Hour Exited : "))
tem = int(input("Minute Exited : "))
tes = int(input("Second Exited : "))
print("Time Entered (camera1)", tsh, ":", tsm, ":", tss, "and Time Exited (camera2)", teh, ":", tem, ":", tes)
if tsh < teh:
tm = (((teh - tsh)*60) + (tem - tsm) +((tes - tss)/60))/60
elif tsh > teh:
teh = 24 + teh
tm = (((teh - tsh)*60) + (tem - tsm) +((tes - tss)/60))/60
speed = run/tm
print("speed of", reg_num, "is", "{:.2f}".format(speed), "mph")
if speed > 70:
list1.append(reg_num)
break
print("Overspeeding vehicles are: ")
for item in list1:
print (item)
this is the code to calculate the speed of a vehicle that passes through a speed camera set 1 mile apart. i have to out put a list of vehicles that are exceeding the speed limit. the problem is when the code calculates the speed (speed = run/time) the error massage states that "tm"(totalminutes) is not defined. can you make some corrections and tell me what is wrong.
The problem is none of your conditions in the 'if' statement are met.
if tsh = teh:
tm = bla
elif tsh < teh:
tm = bla * 2
else:
tm = bla / 2 # Add the 'else' part to do something when all else fails.

Division by zero error when adding rogue value without any data

Hi having trouble trying to fix an error that occurs when I put just a '#' or rogue value in case someone doesn't want to add any data. I don't know how to fix it and I'm hoping to just end the code just like I would with data.
#Gets Data Input
def getData():
fullList = []
inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
while inputText != "#":
nameList = []
nameList2 = []
nameList = inputText.split()
nameList2.extend((nameList[0],nameList[1]))
nameList2.append((float(nameList[2]) + float(nameList [3]))/2)
fullList.append(nameList2)
inputText = checkInput("Enter the students first name, last name, first mark, and second mark (# to exit): ")
print("\n")
return fullList
#Calculates Group Average
def calc1(fullList):
total = 0
for x in fullList:
total = total + x[2]
groupAverage = total/(len(fullList))
return(groupAverage)
#Finds Highest Average
def calc2(fullList):
HighestAverage = 0
nameHighAverage = ""
for x in fullList:
if x[2] > HighestAverage:
HighestAverage = x[2]
nameHighAverage = x[0] + " " + x[1]
return (HighestAverage, nameHighAverage)
#Returns Marks above average
def results1(groupAverage,r1FullList):
r1FullList.sort()
print("List of students with their final mark above the group average")
print("--------------------------------------------------------------")
print("{:<20} {:<12}".format("Name","Mark"))
for x in r1FullList:
if x[2] > groupAverage:
name = x[0] + " " + x[1]
print("{:<20} {:<12.2f}".format(name,x[2]))
def calc3(x):
if x[2] >= 80:
return 'A'
elif x[2] >= 65:
return 'B'
elif x[2] >= 50:
return 'C'
elif x[2] < 50:
return 'D'
else:
return 'ERROR'
def results2(fullList):
print("List of Studens with their Final Marks and Grades")
print("-------------------------------------------------")
print("{:<20} {:<12} {:<12}".format("Name","Mark","Grade"))
for x in fullList:
grade = calc3(x)
name = x[0] + " " + x[1]
print("{:<20} {:<12.2f} {:<12}".format(name,x[2],grade))
#Checks for boundary and invalid data
def checkInput(question):
while True:
textInput = input(question)
if textInput == "#":
return textInput
splitList = textInput.split()
if len(splitList) !=4:
print("Invalid Format, Please Try Again")
continue
try:
a = float(splitList[2])
a = float(splitList[3])
if float(splitList[2]) < 0 or float(splitList[2]) > 100:
print("Invalid Format, Please Try Again")
continue
if float(splitList[3]) < 0 or float(splitList[3]) > 100:
print("Invalid Format, Please Try Again")
continue
return(textInput)
except ValueError:
print("Invalid Input, Please Try Again")
continue
#Main Program
#Input Data
fullList = getData()
#Process Data
groupAverage = calc1(fullList)
HighestAverage, nameHighAverage = calc2(fullList)
#Display Results
print("The group average was %.2f" % groupAverage)
print("The student with the highest mark was: %s %0.2f" %(nameHighAverage,HighestAverage))
results1(groupAverage,fullList)
print("\n")
results2(fullList)
Your program works OK for me, unless you enter a # as the first entry, in which case fullList is [] and has length 0. Hence, DivisionByZero at this line: groupAverage = total/(len(fullList)).
You could modify your code to check for this and exit:
import sys
fullList = getData()
if not fullList:
print('No Data!')
sys.exit()

Categories