0

Currently doing the csnewbs extended task 1 (https://www.csnewbs.com/python-extended-task-1) and I've been struggling with a portion of the code which requires 3 inputs and a confirmation that they were correct. I have the 3 inputs but that's about it.

print("Hello there, welcome to Pete Porker")

while True:
    e = int(input("Scotch eggs are 30p each. How many would you like to order?"))
    if e == "":
        continue
    p = int(input("Pork Pies are 80p each. How many would you like to order?"))
    if p == "":
        continue
    q = int(input("Quiche Tarts are £1.40 each. How many would you like to order?"))
    if q == "":
        continue
print("You have ordered",e,"eggs",p,"pies and",q,"quiches.")
order = input("Is this the right order?")
if order == "yes":
    continue
elif order == "no":
    break

There is a "continue not properly in loop" at the end and I'm not sure how to fix that either. There's an image of what the script should run at the end of the link if that helps. Thanks in advance for all the responses :)

aryaan
  • 3
  • 1
  • It looks like your last `if/else` is backwards. If they answer yes you want to break out of the loop. If they say no you should repeat the loop. – Barmar Jan 05 '21 at 08:29
  • 2
    Also, your check if the input is an empty string is pointless, since you convert the input to int. If you input is an empty string the conversion to int will fail and your program will throw an exeption. – sunnytown Jan 05 '21 at 08:29
  • And if they enter a blank for the third question, you'll go back and ask all the previous questions again. You should probably have a separate loop for each question. – Barmar Jan 05 '21 at 08:30

2 Answers2

0

Your indentation is off. Indent your last if/elif statement so that it's in the while loop, as right now your last continue (and break) isn't contained in any loop.

Alec
  • 8,529
  • 8
  • 37
  • 63
0

You need to continue if break == no, otherwise break (so it's the opposite of what you've done). Your last if-then-else statement is not correctly idented (it should be 1 level further to the right). You cannot check if an integer == "" either.

Also, I wouldn't cycle the entire loop if the amount entered is empty... I'd write a simple function with an internal loop that keeps asking the same question until it gets any non-empty answer... Something like

def query(txt):
    while True:
        r = input(txt)
        if len(r) > 0:
            break
    return int(r)

then your main loop would look like

while True:
    e=query('enter n of eggs')
    p=query('enter n of pies')
    q=query('enter n of quiche')
    A=input('is this enough? (y/n)')
    if A=='y':
        break
Girardi
  • 2,734
  • 3
  • 35
  • 50
  • Thank you that makes a lot more sense now:) . I only needed to check with the end question and not the first 3 because breaking would have made them be answered again. – aryaan Jan 05 '21 at 21:16
  • You're welcome :) Accept the answer if it solved your problem... Cheers – Girardi Jan 06 '21 at 05:02