python - Receive User Input and Test Whether It is a Float or Not -


i trying create function receive input list , justify whether float value or not, , if is, continue program, , if not, ask user enter answer second time. new value should go list @ same index previous, incorrect value.

for example, if inputted code value 'seventy-two' instead of 72, want inputhandler function receive incorrect value, tell user invalid, , ask user answer same question again.

i want function use try-except-else statements. here code:

quiz_grades = int(input("how many quiz grades? "))  program_grades = int(input("how many program grades? "))  tests = int(input("how many tests? "))  def main():      globalconstantlist = [quiz_grades, program_grades, tests]      scorelist = []       returnedscorelist = getgrades(globalconstantlist,scorelist)      returnedscorevalue = inputhandler(returnedscorelist)      returnedscorevalue2 = inputhandler(returnedscorelist)      returnedscorelistsum, returnedscorelistlength = totallist(returnedscorelist)      returnedscorelistaverage = calcaverage(returnedscorelistsum,                                        returnedscorelistlength)      returnedlettergrade = determinegrade(returnedscorelistaverage)      useroutput(returnedscorelistaverage,returnedlettergrade)  def getgrades(globalconstantlist,scorelist):      eachscore in globalconstantlist:      #totalscorelist = 0.0      index = 0      index in range(quiz_grades):          print("what score for", index + 1)          scorelist.append(float(input()))          index += 1      index in range(program_grades):          print("what score for", index + 1)          scorelist.append(float(input()))          index += 1      index in range(tests):          print("what score for", index + 1)          scorelist.append(float(input()))          index += 1      return scorelist  def inputhandler(scorelist):      index = 0      try:          print("what score for", index + 1)          scorelist.append(float(input()))          return scorelist      except valueerror:          print("your value not correct. try again.")          print("what score for", index + 1)          scorelist.append(float(input()))          return scorelist   def totallist(newscorelist):      returnedscorelistlength = len(newscorelist)      returnedscorelistsum = sum(newscorelist)      return returnedscorelistsum, returnedscorelistlength  def calcaverage(newscorelistsum, newscorelistlength):      returnedscorelistaverage = newscorelistsum / newscorelistlength      return returnedscorelistaverage  def determinegrade(newscorelistaverage):      if newscorelistaverage >= 90:         return 'a'      elif newscorelistaverage >= 80:         return 'b'      elif newscorelistaverage >= 70:         return 'c'      elif newscorelistaverage >= 60:         return 'd'      else:        return 'f'  def useroutput(newscorelistaverage, newlettergrade):      print("your overall grade is",format(newscorelistaverage,'.2f'))      print("your letter grade is",newlettergrade)      print() main() 

as far understand, want check user's input , convert float. if successful, want proceed, if not, want ask again.

assuming case, might want write function asks user input, tries convert input float, , returns if successful.

def input_float(prompt):     while true:         try:             inp = float(input(prompt))             return inp         except valueerror:             print('invalid input. try again.')  f = input_float('enter float') print(f) 

you can use snippet starting point further handling of f (which float) user provided.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -