Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to add user input together then print the result
Hi there this will be my first post so I apologize if my formatting is incorrect, I am still learning python :)

My program asks for how many birthday gifts then takes that number and asks for names based on what number the user inputs eg: 3 then asks for 3 names

Then it takes the length of each name, multiplies that by 10 then that becomes the gift budget. eg: Jack has 4 letters so jack's budget will be $40

Basically what I am stuck on is how do add the total of all names budgets together then print it.

Here is my code.

mylist = []
gift = int(input("How many birthday gifts do you need to buy?"))
for number in range(gift):
   names = input("Enter a name:")
for names in mylist: print ("The budget for",names,"'s gift is $",len (names)*10)
And my output looks like this.
How many birthday gifts do you need to buy?3 Enter a name:Jack Enter a name:John Enter a name:Miller ['Jack', 'John', 'Miller'] The budget for Jack 's gift is $ 40 The budget for John 's gift is $ 40 The budget for Miller 's gift is $ 60 Process finished with exit code 0
So it is really just the budget of gifts I am wanting to add together in something like
print("The total budget for gifts is "....")
Any help would be much appreciated as I've been stuck on this for quite some time now :)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
So how would I use this to calculate the length of names as numbers together.

Like I  know how to add the names together such as

print("The total budget for gifts is ", mylist , "")

In other words calculating every len (names)*10 value together
It was a hint.

Join all the names from the list together get the length and multiply by 10
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
Ah ok sorry didn't read the whole post Ill give this a try thanks.

So for example

print("The total budget for gifts is ", len("".join(mylist))*10 , "")

@wavic Thanks heaps it works now I can sleep haha seriously been stuck on that for a few hours now. Thank you :)

def calc_gifts():
    namedict = {}
    names = input("Enter a list of names: ")
    for name in names.split():
        namedict[name] = len(name)*10
    total = 0
    for name, budget in namedict.items():
        print('name: {} will get a ${} gift'.format(name, budget))
        total = total + budget
    print('\nTotal budget is ${}'.format(total))

Enter a list of names: Billy Harry Fred Gail Paula Alfonzo name: Fred will get a $40 gift name: Gail will get a $40 gift name: Paula will get a $50 gift name: Billy will get a $50 gift name: Alfonzo will get a $70 gift name: Harry will get a $50 gift Total budget is $300
Hey thats real nice, that will come in handy thanks heaps.

And also thanks cause now I know how to format a $ before a number properly instead of having $ 50 now its $50

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  how to add the user input from file into list wilson20 8 597 May-03-2020, 10:52 PM
Last Post: Larz60+
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 619 Feb-27-2020, 09:22 AM
Last Post: buran
  Writing a function that changes its answer based on user input SirRavenclaw 2 416 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 588 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 570 Jul-31-2019, 09:09 PM
Last Post: SheeppOSU
  New to Python - tiny coding assistance on user input function and assign to variable Mountain_Duck 1 624 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 557 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 2,075 Mar-02-2019, 10:11 PM
Last Post: johneven
  How to print a statement if a user's calculated number is between two floats Bruizeh 2 577 Feb-10-2019, 12:21 PM
Last Post: DeaD_EyE
  Print The Length Of The Longest Run in a User Input List Ashman111 3 1,017 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk

Forum Jump:

Users browsing this thread: 1 Guest(s)