Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to break the loop?
#1
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')
Reply
#2
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')
Reply
#3
first one works just fine for me
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
in the second one i!=0 does not dow hat you think - it just compares i with 0
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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?
Reply
#6
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: ')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#7
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#9
Thank you so so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Break within an if statement inside a while loop not working Python_more_on 2 1,838 Sep-03-2025, 12:55 AM
Last Post: Pedroski55
  Code won't break While loop or go back to the input? MrKnd94 2 2,739 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 4,633 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  break out of for loop? User3000 3 3,214 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 5,610 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  tkinter control break a while loop samtal 0 3,621 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 6,487 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 11,441 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  break for loop Agusben 1 2,947 Apr-01-2020, 05:07 PM
Last Post: Larz60+
  Help For AutoGui Loop break ahmetnwpal 0 2,984 Mar-11-2020, 01:14 PM
Last Post: ahmetnwpal

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.