Python Forum
Coding help required in Python using VS Code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Coding help required in Python using VS Code
#1
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.
Reply
#2
what about storing items and price in a dictionary, - creating a loop for the question and store the sum in a variable ?
Reply
#3
Information 
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to python/coding Need help on Understanding why this code isn't working. Thanks! mat3372 8 2,103 May-09-2023, 08:47 AM
Last Post: buran
  "SUMIF" type query in Python (help required) BlainEillimatta 0 1,011 Oct-06-2022, 09:08 AM
Last Post: BlainEillimatta
  python 3: TypeError: a bytes-like object is required, not 'str' wardancer84 3 6,857 Jul-09-2021, 05:55 PM
Last Post: deanhystad
  Installing Python and pointing it to required libraries hooiberg 2 4,477 May-13-2019, 05:55 PM
Last Post: ebolisa
  Another working code, help required for faster multithreading execution anna 0 2,365 Feb-09-2018, 03:26 AM
Last Post: anna
  Help required for faster execution of working code anna 2 3,334 Feb-09-2018, 03:00 AM
Last Post: anna
  working code, suggestion required for improvement anna 18 8,963 Dec-29-2017, 01:24 PM
Last Post: buran
  "Python launcher" required to run *.py scripts on Windows? pstein 3 11,555 Jun-27-2017, 02:33 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

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