Python Forum

Full Version: how to break the loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to check all the input i receive, i know i can put float or int at the beginning of the input but i want it to print an error message when error occurs.(a way to do this?)

I used try and except twice.(works but can't use it for each input i receive if there are too many).But the first inputs i didn't use it,is there a way to use it once or another way to check them once and for all. Also all the inputs should be between 0 and 100 how do i write that in a simpler way or just once and not separately for each.



midterm_1=input('Please enter your first midterm score: ')
midterm_2=input('Please enter your second midterm score: ' )
midterm_total= float(midterm_1)*(20/100) + float(midterm_2) * (20/100)
print('your total percentage from midterm scores is: ' + str(midterm_total))
 
summary_paper=input('Please enter your summary paper score: ')
summary_total= float(summary_paper) * (10/100)
print('your total percentage from summary paper is: ' + str(summary_total))
 
response_paper=input('Please enter your response paper score: ')        
response_total= float(response_paper) * (20/100)
print('your total percentage from response paper is: ' + str(response_total))
 
 
i=1
my_list = []
while i <=10:
    try: 
        my_list.append(int(input('please enter your discussions scores: '))) 
        i += 1
     
    except: 
        print('you made an error,please try again')
print(my_list)
 
 
l=[]
for x in my_list: 
    dis_score = x * (2/100)
    l.append(dis_score)
 
print('your total percentage from discussion scores is: '+ str(sum(l)))
 
 
k=1
my_list2 = []
while k <= 5:
    try: 
        my_list2.append(int(input('please enter your top hat scores: '))) 
        k += 1
     
    except: 
        print('you made an error,please try again')
print(my_list2)
 
s=[]
for x in my_list2: 
    tophat_score = x * (2/100)
    s.append(tophat_score)
 
print('your total percentage from discussion scores is: '+ str(sum(s)))
     
total_sum = midterm_total + summary_total + response_total + sum(l)+ sum(s)
 
if total_sum >= 86:
    print('You received an A with total score of ' +str(total_sum))
     
elif total_sum>=74 and total_sum<=85 :
    print('You received a B with total score of ' +str(total_sum))
 
elif total_sum >=62 and total_sum<=73:    
    print('You received an C with total score of ' +str(total_sum))
 
elif total_sum >=54 and total_sum <=61:
    print('You received an D with total score of ' +str(total_sum))
 
else: 
    print('you failed the course')
This runs forever even though i used break :(
while True:
    try:
    
        midterm_1=float(input('Please enter your first midterm score: '))
        midterm_2=float(input('Please enter your second midterm score: ' ))
        midterm_total= midterm_1*(20/100) + midterm_2 * (20/100)
        print('your total percentage from midterm scores is: ' + str(midterm_total))
    
        summary_paper=float(input('Please enter your summary paper score: '))
        summary_total= summary_paper * (10/100)
        print('your total percentage from summary paper is: ' + str(summary_total))
    
        response_paper=float(input('Please enter your response paper score: '))        
        response_total= response_paper * (20/100)
        print('your total percentage from response paper is: ' + str(response_total))
        break
        
    except:
        print('please enter a valid number')

This doesn't work as well, what am i doing wrong? Please help :(
i=0
while i==0:
    try:
    
        midterm_1=float(input('Please enter your first midterm score: '))
        midterm_2=float(input('Please enter your second midterm score: ' ))
        midterm_total= midterm_1*(20/100) + midterm_2 * (20/100)
        print('your total percentage from midterm scores is: ' + str(midterm_total))
    
        summary_paper=float(input('Please enter your summary paper score: '))
        summary_total= summary_paper * (10/100)
        print('your total percentage from summary paper is: ' + str(summary_total))
    
        response_paper=float(input('Please enter your response paper score: '))        
        response_total= response_paper * (20/100)
        print('your total percentage from response paper is: ' + str(response_total))
        i!=0
        
    except:
        print('please enter a valid number')
first one works just fine for me
in the second one i!=0 does not dow hat you think - it just compares i with 0
The only problem i realized is that whenever the user makes a mistake, it starts from the beginning. i just want it to ask the last question again, but putting try and except for each input is hard, there must be a simpler way?
Then make a function to ask questions and return only if the entered value is a float.
By the way, never use a single except.
For example, float, int etc. will raise a ValueError if the provided value is not valid.

def ask_float(question):
    while True:
        try:
            value = float(input(question))
        except ValueError:
            # optional print to give the user a message, that the value was not valid
            continue
        else:
            return value


val1 = ask_float('Please enter your first midterm score: ')
val2 = ask_float('Please enter your second midterm score: ')
yes - define a function that will take prompt as input and will ask user until gets a valid float.
This will make it even ea

def ask_user(prompt, conversion=float):
    while True:
        try:
            return conversion(input(prompt))
        except ValueError:
            print('please enter a valid number')

score = ask_user('Please enter your midterm score')
you can pass any conversion function you want (e.g. int), even custom one.
Then you can think to store prompts in a list, tuple and use a loop to ask for all inputs
It doesn't address what your asking but,
You could replace this:
i=1
my_list = []
while i <=10:
    try: 
        my_list.append(int(input('please enter your discussions scores: '))) 
        i += 1
      
    except: 
        print('you made an error,please try again')
print(my_list)
with this:
mylist = input('Enter something seperated by comma: ').split()
Output:
Enter something seperated by comma: 12, 15, 99 >>> mylist ['12,', '15,', '99']
A little easier to work with list
You may also find what your looking for in this post
Thank you so so much!