0
print("Hi im a PC and my name is Micro, What's your name?")
name = raw_input("")
print("Hi " + name + " how are you, are you good?")
answer = (raw_input(""))
if answer == "yes":
           print("That's good to hear!")
elif answer == "no":
           print("Oh well")
while answer != ("yes","no")
           print("Sorry, you didnt answer the question properly, Please answer with a yes or no.")
print"I'm going to sleep for 5 seconds and then i'll be back."
import time
time.sleep(5)
print"I'm back!"

need to create a loop for the yes or no bit, anyone know how? thanks for any help!

1
  • 1
    It's okey to be new at something. But may I suggest you look thru these serie of videos: youtube.com/watch?v=tKTZoB2Vjuk&ob=av3e or something similar? That will take you a few hours but it will be totally worth it. You will get some basic understanding of how things work :) Commented Jan 7, 2012 at 9:44

2 Answers 2

1

Use while True: and when you want to stop the loop use break.

This would be your code than:

...
while True:
    answer = (raw_input(""))
    if answer == "yes":
        print("That's good to hear!")
        break
    elif answer == "no":    
        print("Oh well")
        break
    else:
        print("Sorry, you didnt answer the question properly, Please answer with a yes or no.")
...
Sign up to request clarification or add additional context in comments.

1 Comment

sorry im really new and although iv heard of both of these i havent yet learnt them. would you care to explain or give an example
1

And now for a completely different thing:

options = {'intro':"Hi, I'm a PC and my name is Micro, What's your name? > ",
           'ask':  "Hi %s how are you, are you good? > ",
           'yes':  "That's good to hear!",
           'no':   "Oh well",
           'error':"Sorry, you didnt answer the question properly\n",
           'hint': "Please answer with yes/no"}

name = raw_input(options['intro'])

while True:
    try:
        answer = raw_input(options['ask'] % name)
        print options[answer.lower()]
        break
    except KeyError:
        print options['error'], options['hint']    

As you said you are a noob in Python, I wanted to introduce here several new things to complement the other answer you may find useful.

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.