简体   繁体   中英

How to ask user's name with the use of def function more efficiently

I've basically made a def function to ask user's name with the input but when I tried to make an input to get the user's name accordingly, I came to realise that each input has to be assigned individually so as to later print that input linked according to what the user inserted their name.

## Ask user's name with the use of def function.

def asking_username():
        print("What's your name? ")
        username = input()
        return username

print("Hello, " + asking_username().title() + "!")

The code above that I made has no problem, but I'd like to make the input to get an user insert in the same line as the print("What's your name? ").

How do you individually assign each input so that it does not get confused with the other input in case multiple inputs are inserted?

How about:

username = input("Enter your name\n")

This works, because I'm using the newline character ( "\\n" ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM