Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to add user input together then print the result
#1
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:")
    mylist.append(names)
print(mylist)
for names in mylist: print ("The budget for",names,"'s gift is $",len (names)*10)
And my output looks like this.
Output:
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 :)
Quote
#2
"".join(mylist)*10
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Quote
#3
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

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

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

len("".join(mylist))*10
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."
https://freedns.afraid.org
Quote
#5
Ah ok sorry didn't read the whole post Ill give this a try thanks.

So for example

len("".join(mylist))*10
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 :)
Quote
#6
Try:

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))

calc_gifts()
result:
Output:
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
Quote
#7
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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Print the longest str from user input edwdas 5 149 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 292 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 346 Mar-23-2019, 06:54 PM
Last Post: Yoriz
  Extracting list element with user input valve 1 331 Mar-11-2019, 07:37 PM
Last Post: Yoriz
  turtle polygon as specified by user input johneven 7 798 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 360 Feb-10-2019, 12:21 PM
Last Post: DeaD_EyE
  Print The Length Of The Longest Run in a User Input List Ashman111 3 752 Oct-26-2018, 06:56 PM
Last Post: gruntfutuk
  Looping unknowns with user input hw question Turkejive 4 873 Sep-30-2018, 04:57 PM
Last Post: Turkejive
  Using user input to restart and quit a game. Maximuskiller78 4 1,354 Sep-17-2018, 04:23 PM
Last Post: gruntfutuk
  Converting user input to integers (retitled) rwood1875 1 919 Jun-17-2018, 04:15 AM
Last Post: Mekire

Forum Jump:


Users browsing this thread: 1 Guest(s)