I have a Python question.

Asked 2 years ago, Updated 2 years ago, 50 views

aa=[5000,2000,3000,4000,1000]
c=aa[0]
d=aa[1]
e=aa[2]
f=aa[3]
g=aa[4]

print ('1. Cabbage')
print('2.mu')
print('3. lettuce')
print ("4. Pepper")
print('5. cucumber')
b=int(input('Enter Product Number'))

while b>0:
    if b==1:
        print(c)
        b=int(input('Enter Product Number'))
    elif b==2:
        print(d)
        b=int(input('Enter Product Number'))
    elif b==3:
        print(e)
        b=int(input('Enter Product Number'))
    elif b==4:
        print(f)
        b=int(input('Enter Product Number'))
    else:
        break
print ("End")

I made it up to here, but it doesn't work after that.

I want to add the value I choose until I enter 0 in the above WHILE statement.

ex) When entering 1,2,4,0 (else value) = 11000 (5000+2000+4000), exit as listed above
       2,5,0 (else value) input = 3000 (2000+1000) as listed above, exit

python idle

2022-09-21 19:46

2 Answers

The code in the question is often repeated and is not concise.

In Python, if there are multiple if statements, it is more efficient to use dict.

print is also available with the sep option, which makes it much simpler if you actively use of it actively.

c, d, e, f, g = [5000,2000,3000,4000,1000]
product_price = {'1':c, '2':d, '3':e, '4':f}

product_name = ('1. cabbage', '2. radish', '3. lettuce', '4. red pepper', '5. cucumber')
print(*product_name, sep = '\n')

cart_sum = sum(product_price[num] for num initer(lambda:input('Enter Product Number'), '0'))))
print (cart_sum, 'end', sep = '\n')


2022-09-21 19:46

Thank you I just learned Python, so I don't know much about functions. I'm trying to make it with my poor skills, but thank you for your help.


2022-09-21 19:46

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.