Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

how to assign/put a value to an index in a list in python

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Ekta Singhania

User

( 8 months ago )

 

I'm creating this program in python that calculates you total cost according to how much you choose. so the program is pizza delivery, and the user has to choose their choice of pizza out of the menu. what I cant figure out is that how to tell python that whatever the users first,second and third choice is, the cost will be 3 dollars and the rest would be 5 dollars. what I mean in more dept would be that how do i tell python that the users 1st,2nd,3rd choice/input = $3.

I tried writing: menu[0:3] = 3 but that just changes the food in the array/list.

usr_profile.png
Garry Buttler

User

( 8 months ago )

Try this function:

def price(choice):
    if choice <= 3:
        cost = 3
    else:
        cost = 5
     return cost

price(4)

> 5

usr_profile.png
Sophia Chang

User

( 8 months ago )

You can create a counter variable for tracking inputs, increment it after taking each input. And then in the price section of your code, use an if statement to set price to 3 if counter < 4, else set price to 5.

# Create counter variable
counter = 0

# Taking user input code here
counter = counter + 1

# Later when setting price in code
if counter < 4:
    price = 3
else:
    price = 5

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif