Python Forum
Coding help required in Python using VS Code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Coding help required in Python using VS Code (/thread-42398.html)



Coding help required in Python using VS Code - KakashiSenpai - Jun-30-2024

Hi everyone,

I am learning Python and need some help please on how to write the code for my program to do the following:

Customer would like to add item A for $20?: (yes/no user input required here)
Customer would like to add item B for $55?: (yes/no user input required here)
Customer would like to add item C for $10: (yes/no user input required here)
Customer would like to add item D for $5: (yes/no user input required here)
Total cost of items added = $ (display total amount for items above that were had a 'yes' user input response)


Any suggestions would be appreciated.


RE: Coding help required in Python using VS Code - gerpark - Jul-01-2024

what about storing items and price in a dictionary, - creating a loop for the question and store the sum in a variable ?


RE: Coding help required in Python using VS Code - PyDev - Jul-03-2024

How about programming it this way?

#import GUI - easyGUI (download with pip required, command - "pip install easygui")
from easygui import *

#Creating dict item: cost
items={'A': 20, 'B': 55, 'C': 10, 'D': 5}

#Creating list, where will be items selected by user
items_selected=[]

#This code will create the pop-up window, which will get the information: Would user like to add item A, next, save the answer in variable, and finally, 
#add the item if user want to add it
x=ynbox('Do you want to add item A (cost: 20$)?', 'App')
if x:
    items_selected.append('A')

#The same with other items
x=ynbox('Do you want to add item B (cost: 55$)?', 'App')
if x:
    items_selected.append('B')

x=ynbox('Do you want to add item C (cost: 10$)?', 'App')
if x:
    items_selected.append('C')

x=ynbox('Do you want to add item D (cost: 5$)?', 'App')
if x:
    items_selected.append('D')

#Variable "result" will contain totall cost of all items selected by user
result=0

#This fragment will add cost of items selected by user to variable "result"
for a in items_selected:
    result+=items[a]

#This window will display total cost of the items selected by user
msgbox('Total cost of item added: '+str(result), 'App')

#Koniec
This way of programming it is shortest, but displays one window to choice items, and the second to display results, not 4 to choice items and the 5th to display results, as the previous app.

#import GUI - easyGUI (download with pip required, command - "pip install easygui")
from easygui import *

#Creating dict item: cost
items={'A': 20, 'B': 55, 'C': 10, 'D': 5}

#This window will be multi choice box - you can select one, two, three or four items at once
choices=multichoicebox('What items would you like to add?\n Items cost:\nA - 20$\nB - 55$\nC - 10$\nD - 5$', 'App', ['A', 'B', 'C',  'D'])

#This fragment will add cost of items selected by user to variable "result"
for a in choices:
    result+=items[a]

#This window will display total cost of the items selected by user
msgbox('Total cost of item added: '+str(result), 'App')
Check both programs. I hope I helped. If you have some questions, write to me.