Search code examples
pythonprintingreturnnonetype

Why is Python code returning 'None' when there is no double print requests?


This is the beginning of my program in main(). The first input request is printing 'None' in the space where the user is supposed to type their input. The user is still able to type the input but I can't get rid of the 'None'.

I've tried modifying where the return statement is located at the end of the while statement and that doesn't affect anything (right now it's just been taken out but even when it's included it still returns None). I'm not asking it to print twice (as far as I'm aware).

while True:
    start = input(str(print('Would you like to find out your USA weather forecast? Please enter YES or NO.\n')))
    if start.upper() == 'YES':
        choice()
    elif start.upper() == 'NO':
        print('*'*31)
        print('Thank you for joining us today!')
        print('*'*31)
        exit(0)
    else:
        print('Please enter a valid response.')
        break

Solution

  • You want:

    start = input('Would you like to find out your USA weather forecast? Please enter YES or NO.\n')
    

    What's happening:

    To see what's happening in your code, read it from the innermost function call (print) to the outermost (input). The print function call usually prints something to the screen, but it doesn't return a value, so its default return is None. Then, you call str on that return value. str(None) is "None". Then you pass that value to input, i.e. input("None"). This is what's showing up at your prompt.