2

I am trying to take a list of items, each with different amounts and calculate how many of all of the items are needed, based on user input.

For example, a grocery list for a dinner based on how many guest are coming. (5 carrots, 3 onions, 4 peppers, 2 hot dogs, and 1 pound of hamburger) per guest. So the user will input how many guests will be attending and and the item counts will be multiplied based on the input and print that value.

I am new to python, although I know this is a simple problem and I have covered this before, I have spent the past two days trying to get ready for a test and think that I am over-thinking it, because I cant even come close -- so frustrating knowing that once I see it, or something similar it will click. Any help is appreciated. I know that syntax is wrong and everything is messed up, I just typed this up real quick to illustrate a general idea of what I am trying to do. Thanks again.

groc_list= int(input('Enter number of students: ')
carrots= (groc_list* 5)
onions= (groc_list* 3)
peppers= (groc_list* 4)
hot_dogs= (groc_list* 2)
hamburger= (groc_list* 1)
print('You will need', carrots, 'onions', peppers, 'hot dogs', pounds of hamburger')
1
  • what error are you getting?. there is an extra ' in your print statement that might be causing syntax issue Commented Jun 30, 2020 at 14:20

5 Answers 5

1

This works:

groc_list = int(input("Enter number of students: "))

carrots = groc_list * 5
onions = groc_list * 3
peppers = groc_list * 4
hot_dogs = groc_list * 2
hamburger = groc_list * 1

print(
    f"You will need {carrots} carrots, {onions} onions, {peppers} peppers, {hot_dogs} hot dogs, {hamburger} pounds of hamburger."
)

Probably this will suit you more:

students = int(input("Enter number of students: "))

grocery_multiplier = {
    'carrots': 5,
    'onions': 3,
    'peppers': 4,
    'hot dogs': 2,
    'hamburger': 1,
}

texts = []
for field, multiplier in grocery_multiplier.items():
    texts.append(f"{multiplier * students} {field}")

print('You will need ' + ', '.join(texts))

When you need to add, remove items to be calculated.

Result:

Enter number of students: 5
You will need 25 carrots, 15 onions, 20 peppers, 10 hot dogs, 5 hamburger

Sign up to request clarification or add additional context in comments.

Comments

0

Check if this works. Feel free to ask any question:)

groc_list= int(input('Enter number of students: '))
carrots = (groc_list * 5)
onions= (groc_list* 3)
peppers= (groc_list* 4)
hot_dogs= (groc_list* 2)
hamburger= (groc_list* 1)
print("You will need" , str(carrots)+" " + "carrots ", str(onions)+" "+"onions ", str(peppers)+" "+ "peppers ",str(hot_dogs)+" "+"hot_dogs ", str(hamburger)+" "+ "pounds of hamburger")```

Comments

0

syntax error on line 1, paranthesis was not closed properly.

groc_list= int(input('Enter number of students: '))
carrots= groc_list* 5
onions= groc_list* 3
peppers= groc_list* 4
hot_dogs= groc_list* 2
hamburger= groc_list* 1

print(f'carrots-{carrots}, onions-{onions},peppers-{peppers},hotdogs-{hot_dogs}, hamburgers-{hamburger} ')

Comments

0

In my opinion, you should use a dictionary to track all the variabiles, and use a func. You also miss a round brackets on the first line. Also, at the end, on print, use f. Here is the code that I think is correct

num = int(input('Number of students: '))
mol_dict = {'carrots':5,'onions':3.........)
res_dict = {}
for i in mol_dict:
    res_dict[i] = mol_dict[i] * num
print(f'You will need {res_dict['carrots']}.........')

This is more readable, more correct and scalable.

Comments

0

Your code was basically nearly complete. Made a few minor tweaks:

groc_list = int(input('Enter number of students: '))
carrots = groc_list
onions = groc_list* 3
peppers = groc_list* 4
hot_dogs = groc_list* 2
hamburger = groc_list
print('You will need', carrots, 'carrots,', onions, 'onions,', peppers, 'peppers,', hot_dogs, 'hot dogs and', hamburger, 'hamburgers.')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.