Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help
#1
# Your code below...
name = "  "
number_of_names = int(input("How many names do you have: "))

for i in range(number_of_names):
    next = input("Enter Your Name: ")
    name = name + next
    
print ("Your name is:" + name)
The only problem I'm having is I'm not sure how to get the spaces between the names.

I get the following return:
How many names do you have: 3
Enter Your Name: Bob
Enter Your Name: tim
Enter Your Name: Tom
Your name is: BobtimTom
Reply
#2
name = name + next
The same way you are concatenating two strings here, you can also concatenate a whitespace character, which is still just a string.
Reply
#3
first of all, please stop such non-descriptive names. you are here on the forum long enough to know that most of the people here ask for help. by using more descriptive thread title it will help people that would like and can help you to do so more quickly.
although j.carter's advise is 100% correct it is better to start using more advanced methods like str.format, or even f-strings (available in python 3.6+) and st.join
# Your code below...
names = []
number_of_names = int(input("How many names do you have: "))
 
for i in range(number_of_names):
    name = input("Enter Your Name: ")
    names.append(name)
     
print ("Your name is: {}".format(' '.join(names))
note that it's not good to use built-in functions (like next) as variable names. this way you overwrite it...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

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