0

I just started learning Python. Basically I just want to repeat the loop once if answer is yes, or break out of the loop if answer is no. The return True/False doesn't go back to the while loop?

def userinfo():
    while true:
    first_name=input("Enter your name here:")
    last_name=input("Enter your last name:")
    phonenum=input("Enter your phone number here")
    inputagain=rawinput("Wanna go again")
    if inputagain == 'no':
        return False

userinfo()
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
imapython
  • 1
  • 1
  • 3
  • possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – vaultah Feb 04 '15 at 18:48
  • `return` causes the function to immediately end, you expect it to go back around? – Ryan Haining Feb 04 '15 at 18:56
  • `True` is capitalized. Your while loop body needs to be indented. If it's python3 then use `input()`, if it's python2 use `raw_input`. – Ryan Haining Feb 04 '15 at 18:57

2 Answers2

0

Instead of while true: use a variable instead.

Such as

while inputagain != 'no':
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
jboromir
  • 1
  • 2
0

Instead of looping forever and terminating explicitly, you could have an actual condition in the loop. First assume that the user wants to go again by starting again as 'yes'

again = 'yes'
while again != 'no':
    # ... request info ...
    again = input("Wanna go again?: ")

though this while condition is a bit weak, if the user enters N, n, no or ever no with space around it, it will fail. Instead you could check if the first letter is an n after lowercasing the string

while not again.lower().startswith('n'):

You could stick to your original style and make sure the user always enters a yes-like or no-like answer with some extra logic at the end of your loop

while True:
    # ... request info ...
    while True:
        again = input("Wanna go again?: ").lower()
        if again.startswith('n'):
            return # exit the whole function
        elif again.startswith('y'):
            break # exit just this inner loop
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174