简体   繁体   中英

how to make next input question repeat a number of times based on user input from previous function

I've written this code because I wanted the function to only proceed to the next one if the user has made an input between 1 and 5:

def max_pizza(question):

  error = "Please enter a number between 1 and 5."

  valid = False

  while not valid:

    try:
      response=int(input(question))
      if  1<= response <=5:
        print("-----------------------------------------------------------------")
        return response
    
      else:
        print(error)
   
    except ValueError:
      print(error)

It's just I needed help with how to make a function to repeat question based on input from the last function, as everytime I seemed to try to type in code and run it, it says that function object can't be interpreted as integer, and I can't figure out how to do it.

First of all, I don't see in the example where you call the function max_pizza . Add at the end of the code, if you haven't done this already max_pizza("Anything") .

Second, it works just fine for me (using Python 3.8.6 ). Try to update python version to the newest version. I'm not sure if something is changed to input or int but these 2 things are the only differences for now.

Third, maybe you're calling this function somewhere, then you should post that one too, and the exact error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM