1

I developed this code for Python 3 in order to ask the user if they wish to continue:

def dnv(): 
    b = input('Would you like to try again? Type 1 for "yes" and 0 for "no": ')
    if b == '1':
        return run()
    if b == '0':
        return print('See you later!')

run() is any program's main function. But now I'm taking a course on Python 2 and I've tried to adapt it on an algorithm I took from there, with no success. The objetive was to determine how many days are between two dates. I believe the relevant part is:

def inputs():
    year1 = input('Type the first year: ')
    month1 = input('Type the first month: ')
    day1 = input('Type the first day: ')
    year2 = input('Type the second year: ')
    month2 = input('Type the second month: ')
    day2 = input('Type the second day: ')
    print daysBetweenDates(year1, month1, day1, year2, month2, day2)

def dnv():
    answer = input('Would you like to try again? Type "y" for yes and "n" for no: ')
    if answer == 'y':
        return inputs()
    else:
        print('See you later!')

I get a NameError: name 'y' is not defined (analogue to 'n') though everytime I type y or n. I've also tried to modify if answer == 'y' to:

if answer.lower().startswith('y'):
    return inputs()

Again with no success. What am I missing? Thank you!

2
  • 2
    python2 input() works differently from python3 input(). Try raw_input() instead. Commented Jan 20, 2018 at 16:49
  • It worked! Thank you so much. Commented Jan 20, 2018 at 16:50

3 Answers 3

2

Use raw_input()

answer = input('Would you like to try again? Type "y" for yes and "n" for no: ')
Sign up to request clarification or add additional context in comments.

Comments

1

If you are using the python 2 you need to change the input to:

answer = raw_input("your message here: ")

And you don't need to return a function you can just call it.

if answer.lower().startswith('y'):
    inputs()

Comments

1

if you are using python 3, I recommend this:

b = input('Would you like to try again? Type 1 for "yes" and 0 for "no": ')
   if b == '1':
       return run()
   if b == '0':
       return print('See you later!')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.