So basically i'm stuck with my code and it's for homework and i don't know how to fix the error that i'm getting
"unindent does not match any outer indentation level"
I've tried to fix it myself but i haven't had any success, so i'm reaching out to anyone that would like to help me.
Here's my code
exit = False
while not exit:
#variables
name=str(input("Enter your students name "))
#User ratings for rolleston spirit
DSR=int(input("Enter rating for Develop Self "))
BCR=int(input("Enter rating for Building Communities "))
TFR=int(input("Enter rating for Transforming Futures "))
#If the input is above a certain number, it will print the student is doing well and if an input is below a ceratin number it will print the student needs support in .....
if DSR>=6:
print (name, "is doing well in developing self")
elif DSR<4:
print (name," needs support in developing self")
if BCR>=6:
print (name, "is doing well in Building Communities")
elif BCR<4:
print (name," needs support in Building Communities")
if TFR>=6:
print (name, "is doing well in Transforming Futures")
elif TFR<4:
print (name," needs support in Transforming Futures")
#Function to find the highest number in a list
def maxi(items):
#max finds the highest number in a list
return max(items)
print(name, "highest score was ", maxi([DSR, BCR, TFR]))
#function to get average of a list
def Average(lst):
return sum(lst) / len(lst)
# Creating List
lst = [DSR, BCR, TFR]
average = Average(lst)
# Printing average of the list
print(name, "has an overall rating of ", round(average, 2))
rep=str(input("Do you wish to continue? Y/N "))
if rep=="Y":
print (" ")
elif rep =="N":
exit = True
print(" ")
Expected output of the code.
Enter your students name Name
Enter rating for Develop Self 3
Enter rating for Building Communities 7
Enter rating for Transforming Futures 9
Name needs support in developing self
Name is doing well in Building Communities
Name is doing well in Transforming Futures
Name highest score was 9
Name has an overall rating of 6.33
Do you wish to continue? Y/N
There might be spaces mixed with tabs in your code and you need 4 spaces to indent. Try this :
exit = False
while not exit:
#variables
name=str(input("Enter your students name "))
#User ratings for rolleston spirit
DSR=int(input("Enter rating for Develop Self "))
BCR=int(input("Enter rating for Building Communities "))
TFR=int(input("Enter rating for Transforming Futures "))
#If the input is above a certain number, it will print the student is doing well and if an input is below a ceratin number it will print the student needs support in .....
if DSR>=6:
print (name, "is doing well in developing self")
elif DSR<4:
print (name," needs support in developing self")
if BCR>=6:
print (name, "is doing well in Building Communities")
elif BCR<4:
print (name," needs support in Building Communities")
if TFR>=6:
print (name, "is doing well in Transforming Futures")
elif TFR<4:
print (name," needs support in Transforming Futures")
#Function to find the highest number in a list
def maxi(items):
#max finds the highest number in a list
return max(items)
print(name, "highest score was ", maxi([DSR, BCR, TFR]))
#function to get average of a list
def Average(lst):
return sum(lst) / len(lst)
# Creating List
lst = [DSR, BCR, TFR]
average = Average(lst)
# Printing average of the list
print(name, "has an overall rating of ", round(average, 2))
rep=str(input("Do you wish to continue? Y/N "))
if rep=="Y":
print (" ")
elif rep =="N":
exit = True
print(" ")
Related
I want to make an calculator for average but i'm facing some issues. I want the numbers entered by the users come as a print statement but it is just throwing the last entered value.Here is my code.
numlist = list()
while True:
inp = input(f"Enter a number, (Enter done to begin calculation): ")
if inp == 'done':
break
value = float(inp)
numlist.append(value)
average = sum(numlist) / len(numlist)
print(f"The Entered numbers are: ", inp)
print(f"average is = ", average)
Solution
we can print the list
numlist = list()
while True:
inp = input(f"Enter a number, (Enter done to begin calculation): ")
if inp == 'done':
break
value = float(inp)
numlist.append(value)
average = sum(numlist) / len(numlist)
print(f"The Entered numbers are: {numlist}")
print(f"average is = ", average)
As I understand, you want to print all the user inputs, however, as I see in your code, you are printing just a value but not the storing list, try to change your code to print(f"The Entered numbers are: ", numlist)
You are telling the user that their entered numbers are the last thing they typed in input, which will always be "done" because that is what breaks out of the loop. If you want to print the entered numbers; print numlist instead of inp.
I am trying to find scores from only 7 judges and to have the numbers between 1-10 only, what is the problem with my code I am an newbie trying to self teach myself:)
So what I'm asking is how to limt the input to have to be 7 inputs per name and to limt the user input to 1-10 for numbers.
import statistics#Importing mean
def check_continue(): #defining check_continue (checking if the user wants to enter more name/athletes
response = input('Would you like to enter a another athletes? [y/n] ') #Asking the user if they want another name added to the list
if response == 'n': #if n, ends the loop
return False
elif response == 'y': # if y, continues the loop
return True
else:
print('Please select a correct answer [y/n]') #making sure the awsener is valid
return check_continue() #calling check continue
while(True):
name = input('Please enter a athletes name: ') #asking for an athletes name
if name == (""):
print ("try again")
continue
else:
try:
print("Enter the judges scores with space inbetween each score") #asking for scores
avg = [float(i) for i in input().split( )] #spliting the scores so the user only needs to put a space for each score
except ValueError:
continue
print("Please enter scores only in numbers")
scores = '' .join(avg)
print("please only enter number")
if scores ==("") or scores <=0 or scores >=10:
print("Please enter a score between 1-10")
continue
else:
print("_______________________________________________________________________________")
print("Athlete's name ",name) #Printing athletes name
print("Scores ", avg) #printing athletes scores
avg.remove(max(avg)) #removing the highest score
avg.remove(min(avg)) #removing the lowest score
avgg = statistics.mean(avg) #getting the avg for the final score
print("Final Result: " +
str(round(avgg, 2))) #printing and rounding the fianl socre
if not check_continue():
break
else:
continue
After reading input scores from judges, make sure that len(input.split()) == 7 to match first criteria.
Once you have the list of scores, just have a check that for each score, 1 <= int(score) <= 10.
If necessary, I can provide a working code too.
import statistics # Importing mean
def check_continue(): # defining check_continue (checking if the user wants to enter more name/athletes
# Asking the user if they want another name added to the list
response = input('Would you like to enter a another athletes? [y/n] ')
if response == 'n': # if n, ends the loop
return False
elif response == 'y': # if y, continues the loop
return True
else:
# making sure the awsener is valid
print('Please select a correct answer [y/n]')
return check_continue() # calling check continue
while(True):
# asking for an athletes name
name = input('Please enter a athletes name: ')
if name == (""):
print("try again")
continue
else:
scores = [float(i) for i in input('Enter the judges scores with space inbetween each score').split() if 1 <= float(i) <= 10]
if len(scores) != 7:
print("try again")
continue
print("_______________________________________________________________________________")
print("Athlete's name ", name) # Printing athletes name
print("Scores ", scores) # printing athletes scores
scores.remove(max(scores)) # removing the highest score
scores.remove(min(scores)) # removing the lowest score
# getting the avg for the final score
avg = statistics.mean(scores)
print("Final Result: " + str(round(avg, 2))) # printing and rounding the final score
if not check_continue():
break
This one should work. Let me know if there are any issues.
scores = [float(i) for i in input('Enter the judges scores with space inbetween each score').split() if 1 <= float(i) <= 10]
This line of code only saves values between 1 and 10, thus already checking for those values. Then the number of elements is checked to be 7.
PS: Would also want to point out that in your code, nothing ever executes after the except ValueError: continue. The continue basically skips to the next iteration without running any of the next lines of code.
My two cents. All checks of input you can warp before printing results with if all(): function for length of input scores list and range of nums between 1 and 10 (see code below).
For mean calc you don't need to delete value from list. You can make sorting and then slicing of list.
check_continue() can be simplify. Check entering for y or n value and return True or False by cases in dict. Otherwise loop.
import statistics
def check_continue():
response = input('Would you like to enter a another athletes? [y/n] ').lower() # Format Y and N to y and n
if response in ['y', 'n']: # Check if response is y or n
return {'y': True, 'n': False}[response]
check_continue()
while True:
name = input('Please enter a athletes name: ') #asking for an athletes name
if name: # Check if name not empty
while True: # make a loop with break at only 7 scores enter
scores_input = input("Enter the judges scores with space inbetween each score (only 7 scores)").split(' ')
try:
scores = [float(s) for s in scores_input] # sort list for extract part for avgg calc
except ValueError:
print("Please enter scores only in numbers")
continue
if all([len(scores) == 7, max(scores) <= 10.0, min(scores) >= 1]):
print("_______________________________________________________________________________")
print(f"Athlete's name {name}") #Printing athletes name
print(f"Scores {scores}") #printing athletes scores
avgg = statistics.mean(sorted(scores)[1:-1]) #getting the avg for the final score
print(f'Final result - {avgg :.2f}')
break
else:
print("Please enter 7 numbers for scores between 1-10")
if not check_continue():
break
For a classroom assignment I have been asked to create a program that averages a number of user input exam grades using a while loop. I have come up with a functioning program, however it does not meet the specific criteria required by my instructor.
I need to Use a while-loop to allow the user to enter any number of exam scores, one number per line, until a sentinel value is entered.
How might I alter this program to get the same result using a while loop and a sentinel value of 9999?
here is what I have so far.
scores=int(input("how many test scores will you enter: "))
total_sum=0
for n in range(scores):
numbers=float(input("Enter exam score : "))
total_sum+=numbers
avg=total_sum/scores
print("average of ", scores, " test scores is :", avg)
The output should look something like this
Enter exam score. 9999 to quit: 100
Enter exam score. 9999 to quit: 95.5
Enter exam score. 9999 to quit: 90
Enter exam score. 9999 to quit: 9999
These 3 scores average to : 95.16666667
Think the best way to do it would be store the scores in a list and then compute the average of them after the user enters the sentinel value:
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("You didn't enter any scores.")
else:
avg = sum(scores)/len(scores)
print("average of ", len(scores), " test scores is :", avg)
There are a couple ways to go about this. The general approach would be to keep a running total of the scores, as well as how many scores you've read, and then check whether the next score to read is == 9999 to see whether or not to exit the while loop.
A quick version might be the following:
num_scores = 0
total_sum = 0
shouldExit = False
while shouldExit is False:
nextScore = float(input("Enter exam score : "))
if nextScore == 9999: #find a way to do this that does not involve a == comparison on a floating-point number, if you can
shouldExit = True
if shouldExit is False:
num_scores += 1
total_sum += nextScore
avg = total_sum / num_scores
See how that sort of approach works for you
You can just break out of your for loop after getting input.
for n in range(scores):
c = int(Input('Enter test score, enter 9999 to break'))
if c == 9999:
break;
scores += c
Something like that at least.
Im really struggling with this question, can anyone help me write a code for this program? or at least say where am I going wrong? Ive tried a lot but can't seem to get the desired output.
This is the program description: Python 3 program that contains a function that receives three quiz scores and returns the average of these three scores to the main part of the Python program, where the average score is printed.
The code I've been trying:
def quizscores():
quiz1 = int(input("Enter quiz 1 score: "))
quiz2 = int(input("Enter quiz 2 score: "))
quiz3 = int(input("Enter quiz 3 score: "))
average = (quiz1 + quiz2 + quiz3) / 3
print (average)
return "average"
quizscores(quiz1,quiz2,quiz3)
One, you are returning a string, not the variable. Use return average instead of return "average". You also don't need the print() statement in the function... actually print() the function.
If you call a function the way you are doing it, you need to accept parameters and ask for input outside the function to prevent confusion. Use a loop as needed to repeatedly use the function without having to rerun it every time. So the final code would be:
def quiz_average(quiz1, quiz2, quiz3):
average = (quiz1 + quiz2 + quiz3) / 3
return average
quiz1 = int(input("Enter Quiz 1 score: "))
quiz2 = int(input("Enter Quiz 2 score: "))
quiz3 = int(input("Enter Quiz 3 score: "))
print(quiz_average(quiz1, quiz2, quiz3)) #Yes, variables can match the parameters
You are returning a string instead of the value. Try return average instead of return "average".
There are a few problems with your code:
your function has to accept parameters
you have to return the actual variable, not the name of the variable
you should ask those parameters and print the result outside of the function
Try something like this:
def quizscores(score1, score2, score3): # added parameters
average = (score1 + score2 + score3) / 3
return average # removed "quotes"
quiz1 = int(input("Enter quiz 1 score: ")) # moved to outside of function
quiz2 = int(input("Enter quiz 2 score: "))
quiz3 = int(input("Enter quiz 3 score: "))
print(quizscores(quiz1,quiz2,quiz3)) # print the result
An alternative answer to the already posted solutions could be to have the user input all of their test scores (separated by a comma) and then gets added up and divided by three using the sum method and division sign to get the average.
def main():
quizScores()
'''Method creates a scores array with all three scores separated
by a comma and them uses the sum method to add all them up to get
the total and then divides by 3 to get the average.
The statement is printed (to see the average) and also returned
to prevent a NoneType from occurring'''
def quizScores():
scores = map(int, input("Enter your three quiz scores: ").split(","))
answer = sum(scores) / 3
print (answer)
return answer
if __name__ == "__main__":
main()
The program will allow the teacher to enter the number of students in her class, and enter the names of each student. Then she will be able to enter the results for one test and find out the percentage scores and a mean average for the class.
This is what I've got so far:
how=int(input("how many students in your class?"))
for i in range (how):
student=str(input("Enter name of student " +str(i+1)+":"))
for n in range (how):
score=int(input("Enter the score of child " +str(n+1)+":"))
outof=int(input("what was the exam out of?"))
print ("The percentage scores are:")
for p in range (how):
print (student,":",(score/outof)*100,"%")
I want it to say each child and each percentage individually. I'd also like to work out the mean of the data.
An example
list_students = []
list_score = []
list_outof = []
how=int(input("how many students in your class?"))
for i in range(how):
student=str(input("Enter name of student " +str(i+1)+":"))
list_students.append(student)
for student in list_students:
score=int(input(" " + student +":"))
list_score.append(score)
outof=int(input("what was the exam out of?"))
list_outof.append(outof)
print("The percentage scores are:")
for index, student in enumerate(list_students):
print (student,":",(list_score[index]/list_outof[index])*100.0,"%")
This is rather simplistic but it should give you enough of a basis to build on for your project.
how = raw_input("how many students in your class?\n")#raw_input() always returns a string
how = int(how)
student_list = []
for student in range(how):
x = raw_input("please input the student's name\n")
y = raw_input("Type the student's score\n")
y = int(y)
student_list.append((x,y))#simple tuple with student's name and score
total =0
for each in student_list:
total+= each[1]
print "The average was " +str(total/float(how))