0

I'm currently in the process of trying to add a while loop to my code that is shown below. The theory behind what I'm attempting to do is as follows:

As you can see at the very bottom of my code, I confirm the user's reservation and ask if he/she would like to create another. If the user enters 'yes", I would like the program to re-run. If no, then the program should terminate. I'm aware the best way to accomplish this is using a while loop, but I'm having a little difficulty executing this as my textbook is a little confusing on the subject.

I know it's supposed to look something like this (or something along the lines of it):

while True:
expression
break

Though I can't seem to get it to compile. Any suggestions? Below is my code:

user_continue = str(raw_input("Your reservation was submitted successfully.  Would you like to do another?"))

if user_continue != 'yes':

print('Thank you for flying with Ramirez Airlines!')
9
  • 1
    Is it really necessary to post the entire code? Commented Mar 5, 2015 at 7:11
  • I apologize, I'm new here. I was just pasting my entire code for context purposes. Do you have a better recommendation? Commented Mar 5, 2015 at 7:13
  • Just post only the code enough to reproduce the actual problem. Check this sscce.org Commented Mar 5, 2015 at 7:14
  • It compiles fine and there is no error to reproduce. I'm just asking for advice on how to add a while loop to my entire code. Commented Mar 5, 2015 at 7:15
  • 1
    Just put the code you want to repeat inside the while loop and at the end of that add something like if user_continue != 'yes': break. Commented Mar 5, 2015 at 7:18

1 Answer 1

1

Here's a simple example that shows how to use a while loop:

import time

while True:
    print time.ctime()
    print 'Doing stuff...'
    response = raw_input('Would you like to do another? ')
    if response != 'yes':
        break

print 'Terminating'

Note that the code inside the while loop must be indented, unlike the code in your first code block. Indentation is very important in Python. Please always ensure that code in your questions (and answers) here is properly indented.

FWIW, the raw_input() input function returns a string, so str(raw_input()) is unnecessary clutter.


The end of your code should look something like:

user_continue = raw_input("Your reservation was submitted successfully.  Would you like to do another?")    
if user_continue != 'yes':
    break

print('Thank you for flying with Ramirez Airlines!')

...

Your print statements are a bit funny. Since you're using Python 2.7 you don't need to do

print ('The total amount for your seats is: $'),user_people * 5180

you can just do

print 'The total amount for your seats is: $', user_people * 5180

or if you wish to use Python 3 style, put everything you're printing inside the parentheses, like this:

print ('The total amount for your seats is: $', user_people * 5180)

However, the output will look a bit messy since there will be a space between the $ and the amount. Please read the python docs to learn how to fix that.

...

Also, you have import time inside your loop. Don't do that. Generally, import statements should be at the top of your script before any other executable code.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the advice! I revised my above code with a while loop but am still receiving an error, do you mind taking a look at my revised code? Thanks for the advice on the raw_inputs as well, I'll fix them!
Your last edit just messed up the main While True: loop! But I'll add some extra info to my answer.
I managed to get my program to loop, but even if the user input isn't 'yes' it'll still print the last statement and just re-start the program. So it works when the input is yes, but not when it's supposed to terminate.
And just like that I managed to fix it. Thank you SO much for your help! The way you explained it helped me understand it a bit better, I really appreciate it!

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.