python - function returns previous value -


i want users input number, check validity , return it. if number invalid, wish repeat input process. if type:

  1. "5b"
  2. "50.4"

the function doesn't return 50.4. instead returns 5.0, remainder of first function call executed. how can fix above issue, 50.4?

def inputgoal ():     goalfiltered = ""     print ('1: ' + str(goalfiltered))     goal = input("input number: ")      char in goal:         matched = false         sign in comparator:             if (sign == char):                 matched = true                 if (char != ','):                     goalfiltered += sign                 elif (char == ','):                     goalfiltered += '.'         if (matched == false):             print ("please repeat input, valid characters are: 0 1 2 3 4 5 6 7 8 9 , .")             inputgoal()             #return     goalfiltered = float(goalfiltered)     return (goalfiltered) 

the comparator variable is:

comparator = ['0','1','2','3','4','5','6','7','8','9',',','.'] 

if understood problem correctly, think can this:

while true:     ask = input("input number:\t") #the \t puts tab looks nice     if ask == float(ask):         break     else:         print("the number incorrect. try again.")         continue 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -