Python Forum
Check all input, easy way! Help pls!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check all input, easy way! Help pls!
#1
while True: try: midterm_1= float(input('Please enter your first midterm: ')) midterm_2= float(input('Please enter your second midterm: ')) 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 score for summary paper: '))) response_paper= float((input('Please enter your score for response paper: '))) writing_total= summary_paper *(10/100) + response_paper *(20/100) print('your total percentage from writing is: ' + str(writing_total)) break except: print('Please enter a valid number') i= 1 my_list= [] while i<=10: try: my_list.append(int(input('Please enter your discussion score: '))) i+=1 except: print('Please enter valid number: ') print(my_list) dis_total=[] for x in my_list: dis_total.append(x*(2/100)) print('your total percentage from discussion is: ' + str(sum(dis_total))) i= 1 top_hat= [] while i<=5: try: top_hat.append(int(input('Please enter your top hat score: '))) i+=1 except: print('Please enter valid number: ') print(top_hat) top_hat_total=[] for x in top_hat: top_hat_total.append(x*(2/100)) print('your total percentage from top hat is: ' + str(sum(top_hat_total))) total_sum = midterm_total + writing_total + sum(dis_total) + sum(top_hat_total) if total_sum >0 and total_sum<100: if total_sum >= 86: print('You received an A') elif total_sum >= 74: print('You received a B') elif total_sum >= 62: print('You received a C') elif total_sum >= 54: print('You received a D') else: print('You failed :(') else: print('Something is not right!') 
What i want to do is to check each input to see if it is between 0 and 100, if not ask to enter again. What is the easiest way to do this? Thanks!
Reply
#2
Anything that is repetitive is a candidate for a function. Below is a function vinput that takes the prompt as an argument and returns the validated input.

def vinput(prompt) : valid = False while not valid : try : temp = float(input(prompt)) if temp<0 or temp>100: raise ValueError else : valid = True except ValueError : print('Invalid entry, try again') return temp midterm1 = vinput('Enter score for midterm: ')
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with to check an Input list data with a data read from an external source sacharyya 3 2,621 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Can I check multi condition for 1 item in a easy way? korenron 4 3,090 May-01-2022, 12:43 PM
Last Post: deanhystad
  I want to check if the input is str or is int & if it's str repeat the loop HLD202 4 4,755 Nov-23-2020, 11:01 PM
Last Post: perfringo
  input check help! bntayfur 3 3,859 Jun-02-2020, 11:38 PM
Last Post: bowlofred
  how i can check the input type? Firdaos 3 4,241 Dec-13-2018, 11:39 PM
Last Post: wavic
  Validating Input (basic check for int etc) gruntfutuk 1 3,644 Aug-06-2018, 07:43 AM
Last Post: gruntfutuk
  check if input is correct before proceeding Pedroski55 1 4,082 Sep-16-2017, 01:56 AM
Last Post: rdx

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.