Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #54154 > unrolled thread
| Started by | mnishpsyched <mnish1984@gmail.com> |
|---|---|
| First post | 2013-09-13 23:34 -0700 |
| Last post | 2013-09-14 14:42 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
Need help to sort out the below code... mnishpsyched <mnish1984@gmail.com> - 2013-09-13 23:34 -0700
Re: Need help to sort out the below code... Terry Reedy <tjreedy@udel.edu> - 2013-09-14 03:50 -0400
Re: Need help to sort out the below code... Jugurtha Hadjar <jugurtha.hadjar@gmail.com> - 2013-09-14 09:38 +0100
Re: Need help to sort out the below code... John Gordon <gordon@panix.com> - 2013-09-14 14:42 +0000
| From | mnishpsyched <mnish1984@gmail.com> |
|---|---|
| Date | 2013-09-13 23:34 -0700 |
| Subject | Need help to sort out the below code... |
| Message-ID | <9f6d4a88-4ae4-4e61-9f73-c074ec3f487f@googlegroups.com> |
Hello guys,
i am new to programming and trying to solve this small coding: my purpose is to take in values from the user based on a menu provided for selection
the output looks like this...
please select from the following menu:
1. pizza
2. steak
3. pasta
4. burger
type in any number from above for selection..
you have selected: 1. Pizza
The bill amounted for Pizza is $20 along with tax of $0.15 will make upto $20.15
the code i have written for this is as follows:
print """
Please select from the following menu:
1. pizza
2. steak
3. pasta
4. burger
type in any number from above for selection..
"""
pz = raw_input()
pz = int(pz)
st = raw_input()
st = int(st)
ps = raw_input()
ps = int(ps)
bg = raw_input()
bg = int(bg)
if pz == 1 :
print "You have selected", pz, ".pizza"
pr_pz = 20
tx_pz = 0.15
tot = pr_pz + tx_pz
print "The bill amounted for Pizza is $", pr_pz, "along with tax of $", tx_pz, "will make upto $", tot
elif st == 2 :
print "You have selected", st, ".Steak"
pr_st = 40
tx_st = 0.20
print "The bill amounted for Steak is $", pr_st, "along with tax of $", tx_st, "will make upto $", tot.....................
...........................
but my program doesn't output the selection once i type in any number from the list..Please help me to sort it out...
[toc] | [next] | [standalone]
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Date | 2013-09-14 03:50 -0400 |
| Message-ID | <mailman.372.1379145023.5461.python-list@python.org> |
| In reply to | #54154 |
On 9/14/2013 2:34 AM, mnishpsyched wrote:
> Hello guys,
> i am new to programming and trying to solve this small coding: my purpose is to take in values from the user based on a menu provided for selection
>
> the output looks like this...
>
> please select from the following menu:
> 1. pizza
> 2. steak
> 3. pasta
> 4. burger
> type in any number from above for selection..
>
> you have selected: 1. Pizza
> The bill amounted for Pizza is $20 along with tax of $0.15 will make upto $20.15
>
> the code i have written for this is as follows:
>
> print """
> Please select from the following menu:
> 1. pizza
> 2. steak
> 3. pasta
> 4. burger
> type in any number from above for selection..
> """
>
> pz = raw_input()
> pz = int(pz)
>
> st = raw_input()
> st = int(st)
>
> ps = raw_input()
> ps = int(ps)
>
> bg = raw_input()
> bg = int(bg)
>
There should only be one input statement:
choice = int(raw_input())
The program now requires 4 inputs, which is not what you want.
Since you are starting, consider using 3.3 insteadd of Python 2 unless
you have a very good reason.
> if pz == 1 :
if choice == 1:
etc.
> print "You have selected", pz, ".pizza"
> pr_pz = 20
> tx_pz = 0.15
> tot = pr_pz + tx_pz
> print "The bill amounted for Pizza is $", pr_pz, "along with tax of $", tx_pz, "willmake upto $", tot
Take out 'amounted'
> elif st == 2 :
> print "You have selected", st, ".Steak"
> pr_st = 40
> tx_st = 0.20
> print "The bill amounted for Steak is $", pr_st, "along with tax of $", tx_st, "will make upto $", tot.....................
> ...........................
> but my program doesn't output the selection once i type in any number from the list.
Because it is waiting for 3 more inputs. See above. Now, you can shorten
and improve the program after the one input statement.
data = (
('Pizza', 20.0, 0.15),
('Steak', 40.0, 0.20),
('Pasta', 14.0, 0.12),
('Burger', 23.0, 0.17),
)
if 1 <= choice <= len(data):
product, price, tax = data[choice]
bill = "The bill for {} is {}, along with tax of {}, total {}"
print(bill.format(product, price, tax, price + tax))
else:
print('I do not know what you mean.')
print() is for 3.x, but works here for 2.x also.
--
Terry Jan Reedy
[toc] | [prev] | [next] | [standalone]
| From | Jugurtha Hadjar <jugurtha.hadjar@gmail.com> |
|---|---|
| Date | 2013-09-14 09:38 +0100 |
| Message-ID | <mailman.373.1379147907.5461.python-list@python.org> |
| In reply to | #54154 |
Hello, mnishpsyched.. You also want the VAT to be computed as a percentage _of_ something, and not hard-coded, so that if you change the price of the pizza, you only have to change one value (pr_pz) instead of two values pr_pz and tx_tz (as the second is computed from the first). You only multiply by coefficients, and these coefficients (the percentage the state charges) is less likely to change than the costs of ingredients or your margins (money moves faster than legislation). -- ~Jugurtha Hadjar,
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2013-09-14 14:42 +0000 |
| Message-ID | <l11slb$ekd$1@reader1.panix.com> |
| In reply to | #54154 |
In <9f6d4a88-4ae4-4e61-9f73-c074ec3f487f@googlegroups.com> mnishpsyched <mnish1984@gmail.com> writes:
> print """
> Please select from the following menu:
> 1. pizza
> 2. steak
> 3. pasta
> 4. burger
> type in any number from above for selection..
> """
> pz = raw_input()
> pz = int(pz)
> st = raw_input()
> st = int(st)
> ps = raw_input()
> ps = int(ps)
> bg = raw_input()
> bg = int(bg)
> but my program doesn't output the selection once i type in any number
> from the list..Please help me to sort it out...
That's because you're calling raw_input() four times, thus the user must
type in a number four times.
You probably want something like this:
user_choice = raw_input()
user_choice = int(user_choice)
if user_choice == 1:
print "You have selected", user_choice, ".pizza"
....
....
elif user_choice == 2:
print "You have selected", st, ".Steak"
....
....
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web