Question

So this is my code:

name = input("So, what is your name? ")
    if name.isalpha():
    print ("So your name is " +(name) + "? Awesome.")
    else:
    print ("That's not a real name! Try again...")

I want to figure out how to return to the original question after the player doesn't type in a real name. This is my first day of using Python (and any language, really) so don't use anything too advanced for me :)

Also, how can I make the first letter of the players name automatically capitalize if it isn't already?

Était-ce utile?

La solution

You can use while loop like this

name = input("So, what is your name? ")
while not name.isalpha():
    print ("That's not a real name! Try again...")
    name = input("So, what is your name? ")
print ("So your name is " +(name) + "? Awesome.")

First, it gets the name, and the while loop makes sure that as long as the name not only has alphabets, it will print That's not a real name! Try again... and take input for name. If the name has only alphabets it automatically breaks out of the loop and prints "So your name is <name entered>? Awesome."

Edit: To make the first character an uppercase letter, you can use title function and instead of concatenating strings, you can format them like this.

print ("So your name is {}? Awesome.".format(name.title()))
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top