Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loops Driving Me Insane
#10
The problem with this approach that i often see is lack of structure.
What i mean that eg loop,menu,calculation,display result...ect all get messed in together.
Structure can be using functions and let then menu only to menu stuff.
When calculate call a function outside of menu.
To give a example of what mean bye this.
def calories_calc(choice):
    candy_calories = {
        "1" : 100,
        "2" : 250,
        "3" : 600,
        "4" : 400,
        "5" : 325,
    }
    total = candy_calories.get(choice, 'Wrong vaule')
    print(f'Calories for this meal was {total}')
    input('Enter to retun to menu\n')

def candys():
    return '''\
    1. Snickers Ice Cream Bar
    2. Nutter Butters
    3. Twix
    4. Twizzlers
    5. Ice Cream Sandwich
    Q. Quit'''

def menu():
    while True:
        print(candys())
        choice = input('Enter your choice: ').lower()
        if choice in '12345':
            calories_calc(choice)
        elif choice == 'q':
           return
        else:
            print(f'Not a correct choice: {choice},try again')

if __name__ == '__main__':
    menu()
Test:
Output:
1. Snickers Ice Cream Bar 2. Nutter Butters 3. Twix 4. Twizzlers 5. Ice Cream Sandwich Q. Quit Enter your choice: 9 Not a correct choice: 9,try again 1. Snickers Ice Cream Bar 2. Nutter Butters 3. Twix 4. Twizzlers 5. Ice Cream Sandwich Q. Quit Enter your choice: 4 Calories for this meal was 400 Enter to retun to menu 1. Snickers Ice Cream Bar 2. Nutter Butters 3. Twix 4. Twizzlers 5. Ice Cream Sandwich Q. Quit Enter your choice: Q
menator01 likes this post
Reply


Messages In This Thread
For Loops Driving Me Insane - by Guybrush3pwood - Apr-29-2022, 07:54 PM
RE: For Loops Driving Me Insane - by deanhystad - Apr-29-2022, 10:12 PM
RE: For Loops Driving Me Insane - by Guybrush3pwood - Apr-29-2022, 11:02 PM
RE: For Loops Driving Me Insane - by deanhystad - Apr-30-2022, 03:00 AM
RE: For Loops Driving Me Insane - by Pedroski55 - Apr-30-2022, 04:59 AM
RE: For Loops Driving Me Insane - by Guybrush3pwood - Apr-30-2022, 12:26 PM
RE: For Loops Driving Me Insane - by Axel_Erfurt - Apr-30-2022, 11:35 AM
RE: For Loops Driving Me Insane - by menator01 - Apr-30-2022, 09:27 PM
RE: For Loops Driving Me Insane - by Pedroski55 - May-01-2022, 05:07 AM
RE: For Loops Driving Me Insane - by snippsat - May-01-2022, 11:01 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with uninstalling Python that is driving me crazy traxus 0 310 Mar-30-2024, 04:37 PM
Last Post: traxus
  how to find difference between fake driving license or real using python? pjaymn 2 2,652 Dec-20-2020, 08:41 PM
Last Post: jefsummers
  Weird behavior with Led Driving code rgkaizen 0 2,771 Jul-14-2017, 04:23 AM
Last Post: rgkaizen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020