4

I'm trying to create a simple script that will will ask a question to which the user will input an answer (Or a prompt with selectable answers could appear?), and the program would output a response based on the input.

For example, if I were to say

prompt1=input('Can I make this stupid thing work?')

I would have something along the lines of

if prompt1='yes': 
    print('Hooray, I can!')

else prompt1='No':
    print('Well I did anyway!')

elif prompt1=#an answer that wouldn't be yes or no
    #repeat prompt1

I'm probably going about this the wrong way. Please be as descriptive as possible as this is a learning exercise for me. Thanks in advance!

1
  • Use == to compare equality, and use the else statement. Commented Nov 2, 2014 at 3:14

2 Answers 2

2

You are pretty close. Read a good tutorial :)

#!python3
while True:
    prompt1=input('Can I make this stupid thing work?').lower()

    if prompt1 == 'yes':
       print('Hooray, I can!')
    elif prompt1 == 'no':
       print('Well I did anyway!')
    else:
       print('Huh?') #an answer that wouldn't be yes or no
  • while True will loop the program forever.
  • Use == to test for equality.
  • Use .lower() to make it easier to test for answers regardless of case.
  • if/elif/elif/.../else is the correct sequence for testing.

Here's a Python 2 version:

#!python2
while True:
    prompt1=raw_input('Can I make this stupid thing work?').lower()

    if prompt1 == 'yes':
       print 'Hooray, I can!'
    elif prompt1 == 'no':
       print 'Well I did anyway!'
    else:
       print 'Huh?' #an answer that wouldn't be yes or no
  • raw_input is used instead of input. input in Python 2 will tries to interpret the input as Python code.
  • print is a statement instead of a function. Don't use () with it.
Sign up to request clarification or add additional context in comments.

4 Comments

I copy/pasted this into my PyCharm, and I get this error when I try to input an answer. (yes or no)
Traceback (most recent call last): File "C:/Users/Shawn/PycharmProjects/helloworld/Test Prograsm.py", line 3, in <module> prompt1=input('Can I make this stupid thing work?').lower() File "<string>", line 1, in <module> NameError: name 'yes' is not defined
You must be using Python 2.x instead. print() is a function in Python 3 so I assumed you are using Python 3. use raw_input in Python 2. Update your question tags to indicate your Python version.
Oh you're right! Completely my fault. Thank you for the learning experience. :)
1

Another example, this time as a function.

def prompt1():
    answer = raw_input("Can I make this stupid thing work?").lower()
    if answer == 'yes' or answer == 'y':
        print "Hooray, I can!"
    elif answer == 'no' or answer == 'n':
        print "Well I did anyway!"
    else:
        print "You didn't pick yes or no, try again."
        prompt1()

prompt1()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.