Home > database >  How to add two user data inputs in python?
How to add two user data inputs in python?

Time:08-31

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

CodePudding user response:

greeting = (f' Welcome {name}')

Or

greeting = ('Welcome '   name )

CodePudding user response:

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.

CodePudding user response:

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)

  • Related