I was doing some Python basic exercises and came across this question:
(First, in my code what did I do wrong, and was my thought process in the right direction?)
Q: Write a Python program that accepts the user’s first and last name and prints them in reverse order with a space between them.
My answer was the following I had an idea but couldn’t think of the correct way/code/format to answer the question. ( I didn’t look up any code b/c I wanted to see if I could do it without help, not yet lol)
My thought process was to create a function (I believe I might have overthought this process)
this is the code I came up with so far. (I couldn’t think of the code to defy the function) I wanted to defy the user name and last name and have the user put in said information then have it print it out but in reverse.
User_Last_name =
User_first_name =
def User_name
print(f"User_Last_name {User_Last_name}, User_first_name {User_first_name}")
input(“Please enter your name!”)
this is the answer the site gives for the problem.
fname = input("Input your First Name : ")
lname = input("Input your Last Name : ")
print ("Hello " + lname + " " + fname)
First off, please post your code, formatted, as the indentation is a critical factor of its operation.
That said, I’m having some trouble in understanding what you want as the output, so maybe you could post something that shows what you want – a picture is worth a thousand words, as the saying goes, but I don’t mean that in a literal sense, rather post something like:
I want the given name here
I want the family name here
Rob thank you for taking the time much appreciated.
I am on a website that gives you a bunch of Python practice tests.
one of the questions was the following.
Q: Write a Python program that accepts the user’s first and last name and prints them in reverse order with a space between them.
I am taking an online Python course and we are learning about Functions. And I figured this was a perfect question to answer with a function. However, I could not code it correctly and it didn’t seem to make it easier.
the answer to the questions is the following.
fname = input("Input your First Name : ")
lname = input("Input your Last Name : ")
print ("Hello " + lname + " " + fname)
I wanted to know from the code I had tried, why I failed what could I have done better, also was the thought process correct. Am I thinking like a coder so to speak?
User_Last_name =
User_first_name =
def User_name
print(f"User_Last_name {User_Last_name}, User_first_name {User_first_name}")
input(“Please enter your name!”)
Coding your own Python functions is a fundamental skill and not too difficult. Functions will make the testing, debugging and maintaining of the code so much easier. Often times, a function can be coded and tested outside of the app for which it was designed and over time you’ll gather together a bunch of useful and cross app functions that you can simply C&P (or better still import, but you’ll get to that, all in good time) for reuse.
Let’s use your first name / last name input exercise as an example.
def get_name():
f_name = input("Input your First Name : ").title() # capitalize the first letter
l_name = input("Input your Last Name : ").title() # capitalize the first letter
return f_name, l_name
def ask_yes_no(question):
error = True
valid_response = ("Y", "N")
while error: # do the following until error is False
response = input(f"{question} (Y)es or (N)o? ").upper() # convert to uppercase
if response in valid_response:
error = False
return response
while True: # do the following until 'break'
print("Please enter your details.")
NAME = get_name()
print(f"You entered {NAME[1]} and {NAME[0]}")
print()
RESPONSE = ask_yes_no('Are you happy with your input')
print()
if RESPONSE == 'Y':
print("Thank you.")
break
As you can see, we already have a useful yes/no function that we can use elsewhere. Also notice that I’ve placed both functions at the head of the script; this is by conversion and makes the script more ‘human friendly’. The one thing missing here (that I can see, least ways) are ‘docstrings’, but you’ll get to those, again, all in good time.
Go through each and every line, making sure that you fully understand what everything is doing and why it’s there: test it, change it, break it and then fix it.