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

General Tech Bugs & Fixes 3 years ago

7.27K 3 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (3)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years 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.

0 views
0 shares

profilepic.png
manpreet 3 years ago

Try this function:

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

price(4)

> 5

0 views   0 shares

profilepic.png
manpreet 3 years 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

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum