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

General Tech Bugs & Fixes . 2 years ago

  0   3   0   0   0 tuteeHUB earn credit +10 pts

5 Star Rating 5 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Write Your Comments or Explanations to Help Others



Tuteehub forum answer Answers (3)


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

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community