Home > Software design >  Using Python for default parameters
Using Python for default parameters

Time:02-04

I am currently learning python and stuck on a coding exercise. I am trying to achieve the result as shown on the image1. I am stuck on the overall code. I also not sure how to incorporate the "quit", so that the program terminates. Image1

def tester(result):

 while tester:
  if len(result)< 10:
   return print(givenstring)
  else:
   return print(result)
  
def main():
 givenstring = "too short"
 result=input("Write something (quit ends): ")
if __name__ == "__main__":
    main()   
     

CodePudding user response:

For your problem, you need to have a variable that is your Boolean (true/false) value and have your while loop reference that. currently your while loop is referencing your function. inside your main function when you get your user input you can have a check that if the input is "quit" or "end" and set you variable that is controlling your loop to false to get out of it.

you also are not calling your tester function from your main function.

CodePudding user response:

You missed into main() function to call your function, like tester(result). But such basics should not be asked here.

def tester(result):
    if len(result)< 10 and result != 'quit':
        givenstring = "too short"
        return print(givenstring)
    else:
        return print(result)

def main():
    result=None
    while True:
        if result == 'quit':
            print("Program ended")
            break
        else:
            result=input("Write something (quit ends): ")
            if result.lower() == 'quit':
                result = result.lower()
            tester(result.lower())
    
if __name__ == "__main__":
    main()
  • Related