Home > Net >  Indentation error? Trying some loops and code keeps bouncing back?
Indentation error? Trying some loops and code keeps bouncing back?

Time:11-26

I am very new to Python and taking a short course to nail down the basics. I tried writing a simple code that goes through some options to ask the user what dessert they have. My problem is that when I run it's looping back on certain parts of the programme, and for others it's not giving the final print statement at all (Your dessert is XYZ). The code is based on the attached Venn diagram, showing a few dessert ingredient combos to give you a final outcome

I have moved the indentations of the code around and still don't get the result I want. After messing about with it for a few days I thought I would ask for help since this is a pretty simple function I am trying to execute and I really don't know why it's taking me so long (apart from the fact that I am on a really steep learning curve). Any help would be appreciated!

print('Let me see what dessert you got!')
answer=input('Is your dessert round fried dough? ')
if answer=='Yes':
    answer2=input('Is it dipped in syrup? ')
    if answer2 == 'Yes':
        print('Your dessert is loukoumades')
    elif answer2 =='No':
        answer3=input('Is it dusted in sugar? ')
        if answer3=='Yes':
            print('Your dessert is a donut')
if answer=='No':
    answer4=input('Is your dessert thin fried dough? ')
    if answer4=='Yes':
            answer3=input('Is it dusted in sugar? ')
            if answer3=='Yes':
                print('Your dessert is churros')
else:
answer5=input('Is it dipped in syrup? ')
if answer5=='Yes':
    print('Your dessert is a jalebi')

CodePudding user response:

Your second if statement that isn't indented should be an elif. This will prevent the flow from going from is it dusted in sugar? -> No -> Is your dessert thin fried dough?. This will also prevent Is is dipped in syrup? from being asked twice. Also, the last three line of your code should be indented.

print('Let me see what dessert you got!')
answer=input('Is your dessert round fried dough? ')
if answer=='Yes':
    answer2=input('Is it dipped in syrup? ')
    if answer2 == 'Yes':
        print('Your dessert is loukoumades')
    elif answer2 =='No':
        answer3=input('Is it dusted in sugar? ')
        if answer3=='Yes':
            print('Your dessert is a donut')
elif answer=='No':
    answer4=input('Is your dessert thin fried dough? ')
    if answer4=='Yes':
            answer3=input('Is it dusted in sugar? ')
            if answer3=='Yes':
                print('Your dessert is churros')
else:
    answer5=input('Is it dipped in syrup? ')
    if answer5=='Yes':
        print('Your dessert is a jalebi')

CodePudding user response:

I tried to stay as close as your original code. Your code is not the most efficient, but once you will be used to python, you will improve it!

print('Let me see what dessert you got!')
answer=input('Is your dessert round fried dough? ')
if answer=='Yes':
    answer2=input('Is it dipped in syrup? ')
    if answer2 == 'Yes':
        print('Your dessert is loukoumades')
    elif answer2 =='No':
        answer3=input('Is it dusted in sugar? ')
        if answer3=='Yes':
            print('Your dessert is a donut')
if answer=='No':
    answer4=input('Is your dessert thin fried dough? ')
    if answer4=='Yes':
            answer3=input('Is it dusted in sugar? ')
            if answer3=='Yes':
                print('Your dessert is churros')
            else:
                answer5=input('Is it dipped in syrup? ')
                if answer5=='Yes':
                    print('Your dessert is a jalebi')
  • Related