-1

Hi Im is create my own text game, it very basic :-) and it´s starting with enter name, the basic flow is working, to have the first letter capitalized in first name, but if capitalize the second letter also, it´s not working, my idea is only allow first letter capitalized, and program should go back if this condition is not fulfilled I was trying to fix this, by adding

elif i != i.upper()[:2]: print("The second letter was capitalized, please retype your name ")

def input_name():  # Take name as parameter and use it later
    first_name = (input(str("What is your first name? ")))
   sur_name = (input(str("What are your surname? ")))
first_capitalized_letter_first_name = first_name
first_capitalized_letter_sur_name = sur_name
for i in first_capitalized_letter_first_name[:1]:
    if i == i.upper():
        print("Good you can start with capitalized letter when you are writing your first name ")
    elif i != i.upper()[:2]:
        print("The second letter was capitalized, please retype your name ")
Hawkeye
  • 1
  • 2
  • Welcome back to Stack Overflow! Please take the [tour] and read [ask]. For debugging help, you need to provide a [mre] including minimal but complete code, example input, expected output, and actual output (or if you get an error, the full error message with traceback). – wjandrea Oct 17 '21 at 16:09
  • It is not clear what do you try to do. post a small code sample and explain. – balderman Oct 17 '21 at 16:10
  • Sorry It was my first question, it was some problems to add the code, but it´s added now – Hawkeye Oct 17 '21 at 16:18
  • 1
    @Hawkeye You're still missing the input, expected output, and actual output (error). – wjandrea Oct 17 '21 at 16:36
  • Does this answer your question? [How can I check if a letter in a string is capitalized using python?](https://stackoverflow.com/questions/4697535/how-can-i-check-if-a-letter-in-a-string-is-capitalized-using-python) – bad_coder Oct 17 '21 at 17:29
  • @Hawkeye if I helped you don't forget accept as answer, or write comment, so I can adjust my answer. – Peter Trcka Oct 17 '21 at 17:33
  • @Peter Trcka Hi Im sorry If i missed to verify the solution, Im newbie in python, so it will take can take some time before I understand how convert to my code, I has not understand how I can use print, into my code, – Hawkeye Oct 18 '21 at 17:14
  • @Hawkeye Understood. In your code you are using foor loop and `if` `else` statements to determine whether there is first capital letter, all other letters are lower case. I have send you a code, where I printed what result `istitle()` function returns for different combination of letter in the word. The prints are there to see that `istitile` returns True only if only first letter is capital. – Peter Trcka Oct 18 '21 at 18:34
  • @Peter Trcka, thanks for your reply, and your solution is working :-) – Hawkeye Oct 18 '21 at 18:52
  • @Hawkeye then accept is as answer :) – Peter Trcka Oct 18 '21 at 18:53
  • @PeterTrcka I tried to vote on your answear but, Im newbie, so I don´t know If should do something more, but your solution is now implemted and tested OK :-) – Hawkeye Oct 18 '21 at 19:00
  • it's closed so no accepting as answer, never mind. Happy to help :) – Peter Trcka Oct 18 '21 at 19:02

1 Answers1

1

str.istitle() is what you are looking for.

The istitle() method returns True if all words in a text start with a upper case letter, AND the rest of the word are lower case letters, otherwise False.

Symbols and numbers are ignored.

print("Ascsf".istitle())  # True
print("AscsF".istitle())  # False
print("SnakeCase".istitle())  # False
print("Ascsf Doo Goo".istitle())  #True

more clear implementation:

your_name = input("What is your name?")
if your_name.istitle():
    print(f"{your_name} is a valid name")
else:
    print(f"{your_name} is not a valid name, plase select another.")
Peter Trcka
  • 1,279
  • 1
  • 16
  • 21