0

I'm new to python but i was just testing this out and it doesn't work.

what i want is for the user to input their name and if it is a string, it will print("Hello" + name) and if it isn't a string it will print("That's not a name")

here is my code:

name = str(input("What is your name? "))
if name != str:
    input("Thats not your name! Please retype your name ")
else: 
    print("Hello " + name)

input("Press any button to close")
  • 1
    I believe the result of `str()` should always be a string value, so "*and if it isn't a string*" doesn't really make sense. Can you provide an example input that should fail the condition? Are you trying to exclude empty strings and/or only whitespace (e.g. no input was actually given)? Or are you expecting a particular value and checking that it matches (in which case, trying to use `str` as both a function and string won't work out well). – Jonathan Lonowski Nov 13 '16 at 19:54
  • Two remarks : **1.** please format your code properly, it is very hard to read as of now **2.** Your question is unclear : do you want to compare the input with an other given string like `"Sam"` or do you want to check the *type* of the variable `name` and see if it is a *string object* ? – jadsq Nov 13 '16 at 19:55
  • @jdasq I have suggested an edit to the code in the question and it is pending peer review. I think by "string" OP means letters only (as a name should contain only letters). However, OP has to confirm this. – Rohin Gopalakrishnan Nov 13 '16 at 19:58
  • also see: http://stackoverflow.com/questions/1549801/differences-between-isinstance-and-type-in-python – NoDataDumpNoContribution Nov 13 '16 at 19:58
  • @RohinGopalakrishnan "Letters only" may be what the OP is after, but they should clarify that themselves. (Though, it at least excludes names with say apostrophes or hyphens, which the OP may want to accept.) – Jonathan Lonowski Nov 13 '16 at 20:03

2 Answers2

1

You can use isalpha function in python to check if the input contains only letters.

This should work:

name = input("What is your name? ") # REMOVED REDUNDANT EXPLICIT TYPE CAST

if name.isalpha(): # CHECK IF IT ONLY CONTAINS LETTERS
    print("Hello " + name)
else:
    name = input("Thats not your name! Please retype your name ") # NOT REQUIRED, BUT SINCE QUESTION WAS TO RE-ENTER

input("Press any button to close")

Hope this helps.

  • Hi @corey pritchard if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Rohin Gopalakrishnan Nov 13 '16 at 20:19
0

The first print statement you see will prompt the user with the question "What is your name?" In the second line of the code,the raw input is then taken from the user and stored in a variable called "name". In the if statement we have a function ".isalpha()" which checks if 'name' is alphabets or not.If it is then it return True and so the first print statement is printed else if its not alphabets then False is returned,hence the print statement after else is printed.

print("What is your name?") 
name = input() 

if name.isalpha(): #
  print("Hello " + name)
else:
  print("Thats not your name! Please retype your name ")
Taufiq Rahman
  • 5,600
  • 2
  • 36
  • 44