I am trying to get the first while True loop to ask a question. If answered with a 'y' for yes, it continues to the second while test_score != 999: loop. If anything other than a y is entered, it should stop.
Once the second while loop runs, the user enters their test scores and enters 'end' when they are finished and the code executes, the first "while True" loop should ask if the user would like to enter another set of test scores. If 'y' is entered, then it completes the process all over again. I know the second one is a nested while loop. However, I have indented it, in PyCharm, per python indentation rules and it still tells me I have indentation errors and would not run the program. That is the reason they are both lined up underneath each other, below. With that said, when I run it as it is listed below, it gives the following answer:
Enter test scores
Enter end to end the input
==========================
Get entries for another set of scores? y
Enter your information below
Get entries for another set of scores? n
((When I type in 'n' for no, it shows the following:))
Thank you for using the Test Scores application. Goodbye!
Enter test score: 80 (I entered this as the input)
Enter test score: 80 (I entered this as the input)
Enter test score: end (I entered this to stop the program)
==========================
Total score: 160
Average score: 80
When I answered 'y', it started a continual loop. When I answered 'n', it did give the print statement. However, it also allowed me to enter in my test scores, input end and execute the program. But, it also did not ask the user if they wanted to enter a new set of test scores. Any suggestions on where I am going wrong?
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'n':
print("Thank you for using the Test Scores application. Goodbye!")
break
else:
print("Enter your information below")
continue
counter = 0
score_total = 0
test_score = 0
while test_score != 999:
test_score = input("Enter test score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)
The while loops should be nested to have recurring entries
print("The Test Scores application")
print()
print("Enter test scores")
print("Enter end to end input")
print("======================")
# initialize variables
counter = 0
score_total = 0
test_score = 0
while True:
get_entries = input("Get entries for another set of scores? ")
if get_entries == 'n':
print("Thank you for using the Test Scores application. Goodbye!")
break
else:
print("Enter your information below")
counter = 0
score_total = 0
test_score = 0
while test_score != 999:
test_score = input("Enter test a score: ")
if test_score == 'end':
break
elif (int(test_score) >= 0) and (int(test_score) <= 100):
score_total += int(test_score)
counter += 1
elif test_score == 999:
break
else:
print("Test score must be from 0 through 100. Score discarded. Try again.")
# calculate average score
average_score = round(score_total / counter)
# format and display the result
print("======================")
print("Total Score:", score_total,
"\nAverage Score:", average_score)
Output:
The Test Scores application
Enter test scores
Enter end to end input
======================
Get entries for another set of scores? y
Enter your information below
Enter test a score: 60
Enter test a score: 80
Enter test a score: 90
Enter test a score: 70
Enter test a score: end
======================
Total Score: 300
Average Score: 75
Get entries for another set of scores? y
Enter your information below
Enter test a score: 80
Enter test a score: 80
Enter test a score: end
======================
Total Score: 160
Average Score: 80
Get entries for another set of scores? n
Thank you for using the Test Scores application. Goodbye!
Related
Currently doing a college assignment where we need to input and add 3 different scores.
The Scores must be not be less than 0 or greater than 10, and they can only be in multiples of 0.5. It's the latter part I'm having trouble with.
How do I tell the program to give an error if the input isn't a multiple of 0.5?
score1 = float(input("Please input the first score: ")
if score1 <0 or score1 >10:
score1 = float(input("Error! Scores can only be between 0 and 10.\n Please input Score 1 again: "))
elif score1
One way to do this is to use a while loop:
score1 = float(input("Please input the first score: ")
# while loop will keep asking for correct input if the input does not satisfy the requirements
while score1 <0 or score1 >10 or not (score1%0.5)== 0.0:
score1 = float(input("Error! Scores can only be between 0 and 10 and in multiples of 0.5.\n Please input Score 1 again: ")
print(score1, "passes all the tests")
or alternatively, you could raise an error statement if you'd like:
score1 = float(input("score: "))
if score1<0 or score1>10 or not (score1%0.5)==0.0:
raise BaseException("Error! the input score1 must be between 1 and 10 and a multiple of 0.5")
print(score1, "passes all the tests")
Nested if statement comes into play here. First it will check the condn is score>0 and score<10 then the next if statement of checking multiple statement comes to play. Use loop for taking input back to back until condn not met.
score = float(input("Enter score"))
if score>0 and score<10:
if (score*10)%5 == 0:
print(" Score is a Multiple of 0.5")
else:
print("Score is Not multiple of 0.5")
else:
print("Error! Scores can only be between 0 and 10")
`
How to create a program that limiting user in input function from 0-100 only. We need to ask user to for their grade in 4 tests after that if the user does not have taken any of those test it shows INC.
while True:
score = float(input('Enter your score: '))
if not 0 <= score <= 100:
continue
else:
break
while True:
score = input('Enter your score: ')
if not (0 <= score <= 100):
continue
else:
break
I am writing a program for a course that takes user input exam scores and averages them. I have a functioning program, however I need to add a function that uses a range to identify scores entered outside of the 1-100 range and display a message "score out of range. please re enter".
Here is what I have so far:
SENTINEL = float(9999)
scores = []
while True:
number = float(input("Enter exam score (9999 to quit): "))
if number == SENTINEL:
break
scores.append(number)
if not scores:
print("No scores entered")
else:
avg = sum(scores)/len(scores)
print("average of ", len(scores), " test scores is :", avg)
Try this:
SENTINEL = float(9999)
scores = []
while True:
number = float(input("Enter exam score (9999 to quit): "))
while number != SENTINEL and (number < 1 or number > 100):
number = float(input("score out of range. please re enter: "))
if number == SENTINEL:
break
scores.append(number)
if not scores:
print("No scores entered")
else:
avg = sum(scores)/len(scores)
print("average of ", len(scores), " test scores is :", avg)
desired outcome:
Enter a test score (-99 to exit): 55
Enter a test score (-99 to exit): 77
Enter a test score (-99 to exit): 88
Enter a test score (-99 to exit): 24
Enter a test score (-99 to exit): 45
Enter a test score (-99 to exit): -99
55 77 88 24 45
P P P F F
Process finished with exit code 0
The code so far: (works except for Pass fail assignment)
Python Program to ask user to input scores that are added to a list called scores. Then prints under that score P for Pass F for fail.
scores = [] #list is initialized
while True:
score = int(input("Enter a test score (-99 to exit): "))
if score == -99:
break
scores.append(score)
def print_scores(): #accepts the list and prints each score separated by a space
for item in scores:
print(item, end = " ") # or 'print item,'
print_scores() # print output
def set_grades(): #function determines whether pass or fail
for grade in scores:
if score >= 50:
print("P")
else:
print("F")
print(set_grades)
You're thinking along the right lines, but you need to run through your program from the top and make sure that you're getting your reasoning right.
Firstly, you've written the program to print out all the scores before you get to checking if they're passes, so you'll get a list of numbers and then a list of P/F. These need to happen together to display properly.
Also, make sure that you keep track of what variable is what; in your last function, you attempt to use 'score', which doesn't exist anymore.
Finally, I'm not sure what exactly you're asking with %d or %s, but you may be looking for named arguments with format(), which are shown below.
scores = [] #list is initialized
while True:
score = int(input("Enter a test score (-99 to exit): "))
if score == -99:
break
scores.append(score)
for item in scores:
if item >= 50:
mark = 'P'
else:
mark = 'F'
print('{0} {1}'.format(item, mark))
I believe this is what you're looking for.
I have no idea what I'm doing wrong, but this is driving me up a wall. Why am I getting a Syntax error? I have to code an average calculator using a while statement, where "0" defines that the program needs to perform the calculation.
Here's the sample output my professor defined:
SAMPLE RUN
Enter test score 80
Enter test score 70
Enter test score 90
Enter test score 88
Enter test score 0
The average is 82.000%
The if statement in the image (please post the actual code in your question) is invalid. It needs a colon, i.e. if test == '0':. It also needs a body.
The sample output suggests that 0 is to terminate the loop. Check for that first, before you modify division_integer otherwise the average will be affected. Then, when you get a 0 break out of the while loop with break, i.e.
while True:
test = int(input('Enter test score:'))
if test == 0:
break
addition_integer += test
division_integer += 1
Note that you are converting the input into an integer, but your test was for the string "0". You need to test for the integer 0 as I show above.
Another point, you should use a try/except block when converting the input into an integer so that you can catch invalid input:
while True:
try:
test = int(input('Enter test score:'))
except ValueError:
print('Invalid number entered, try again.')
continue
# etc.
I've fixed your code for you. For future questions, please post your code directly in your question and indent it all by 4 spaces to properly format it.
addition_integer = 0
divison_integer = 0
while True:
test = int(input("Enter test score: "))
if test == 0:
break
else:
addition_integer += test
divison_integer += 1
print("The average is {}%".format(addition_integer/divison_integer))
Here's an explanation of what I did to fix this:
You were looping while True, which is correct, however you had no way to break out of the while loop. Your line saying if test == '0' would never resolve to true because you're taking the input as an int from the user, not to mention it was out of place and didn't have proper syntax.. What I am doing in the code above is continuing to loop until the input from the user is equal to 0 (an integer, not a string). If the user inputs 0, then we simply break out of the loop and print the average. Until then, we continue to add the input to addition_integer and increment division_integer by 1.
All in all, you were pretty close to the solution, you just needed a few syntax changes and to be steered in the right direction as to how you can break out of an infinite loop.
Finally, here is a test using the numbers that you've provided in your question:
Enter test score: 80
Enter test score: 70
Enter test score: 90
Enter test score: 88
Enter test score: 0
The average is 82%
Alternative answer
Use a list, sum and divide. Same concept, compare test inside the while loop in order to break out of it. Also check if you get valid input that can be made an integer with a try/except.
Additionally, beware of the division by zero
values = []
while True:
try:
test = int(input('Enter test score:'))
if test == 0:
break
values.append(test)
except:
break
total = len(values)
avg = 0 if total == 0 else 1.0 * sum(values) / total
print("The average is", avg)