-1

Write a Python program that takes the user's name as input and displays and welcomes them.

Expected behavior:

Enter your name: John
Welcome John

The Python code for taking the input and displaying the output is already provided

#take the user's name as input
name = input("Enter your name: ")
print(name)



#the vaiable that includes the welcome message is alredy provided.
#Please complete this part using the knowledge obtained in this lesson.
greeting = ("Welcome John")


#print the welcome message
print(greeting)

Out put I got with one error

Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230
  • If you don't know how to do it, you should rewatch the lesson – Yevhen Kuzmovych Aug 30 '22 at 12:45
  • Does this answer your question? [Using multiple arguments for string formatting in Python (e.g., '%s ... %s')](https://stackoverflow.com/questions/3395138/using-multiple-arguments-for-string-formatting-in-python-e-g-s-s) – Gino Mempin Oct 01 '22 at 08:53

4 Answers4

1
greeting = (f' Welcome {name}')

Or

greeting = ('Welcome ' + name )
RaqDeku
  • 27
  • 4
  • You should include details as to why this fixes the poster's issue and what the code examples do. – moken Sep 04 '22 at 11:06
0

The problem I see is an error in your code where you have hard coded the name "John" with the output. What you ought to do instead, is to output the Variable alongside the greeting message.

greeting = (f"Welcome {name}")

Using this will output the welcome message alongwith the name that's entered. What I have done is used an F-string however there's other ways to do it as well. Hope that answer's your question.

  • Could you please give me some more details to fix because I am a beginner for python – A.M. Rilwan Aug 30 '22 at 12:50
  • What more details do you need? You are simply printing the line ```Welcome John``` and you intend to print the user input instead of the word "John" which can only be done through a variable and either f-string literals or string concatenations. – Syed Abdullah Aug 30 '22 at 15:19
0

You have hard coded the greeting

greeting = ("Welcome John")

Given you have a name the user has provided, you can use string formatting to interpolate that name into your greeting like this:

greeting = (f"Welcome {name}")

(Notice, you put the variable in curly braces)

doctorlove
  • 18,872
  • 2
  • 46
  • 62
0
# take the user's name as input
name = input("Enter your name: ")
print(name)

# Please complete this part using the knowledge obtained in this lesson.
greeting = ("Welcome" +" "+ name)

# print the welcome message
print(greeting)
AlexK
  • 2,855
  • 9
  • 16
  • 27