Python Forum
How to add user input together then print the result
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 :)
Reply
#2
"".join(mylist)*10
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
Reply
#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
Reply
#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 :)
Reply
#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
Reply
#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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Simulate an answer based on user input [Beginner needs guidance] Bombardini 1 1,256 Nov-12-2022, 03:47 AM
Last Post: deanhystad
Bug How to print only vowels from an input? PP9044 8 7,427 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Print user input into triangle djtjhokie 1 2,342 Nov-07-2020, 07:01 PM
Last Post: buran
  Changing Directory based on user input paulmerton4pope 13 7,883 Aug-14-2020, 11:48 AM
Last Post: GOTO10
  how to add the user input from file into list wilson20 8 4,229 May-03-2020, 10:52 PM
Last Post: Larz60+
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,497 Feb-27-2020, 09:22 AM
Last Post: buran
  Writing a function that changes its answer based on user input SirRavenclaw 2 2,758 Dec-21-2019, 09:46 PM
Last Post: Clunk_Head
  Print the longest str from user input edwdas 5 4,048 Nov-04-2019, 02:02 PM
Last Post: perfringo
  how to add user input to a dictionary to a graph KINGLEBRON 3 2,979 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 2,464 Mar-23-2019, 06:54 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020