May-01-2022, 11:01 AM
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.
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