-2
    try:
        user_name = str(input("Enter your full name: "))
    except:
        print("Enter a string")

    user_age = int(input("Enter your age: "))
    user_country = str(input("Enter the country you live in: "))
    user_postcode = str(input("Enter your postcode: "))

When I enter an integer for the first one it moves on to the next variable but I want it to say "Enter a string"

4 Answers4

0

Any input is already a string. When you read an integer (such as 123), the input comes as a string (such as "123"). Casting it to str does nothing. You need to be far more specific about what you expect as input, and test for that.

For instance, you might want to determine that all characters are in a particular set -- such as letters, spaces, and certain punctuation marks. Then you'd need to write a particular test or two for those characteristics.

Prune
  • 76,765
  • 14
  • 60
  • 81
0

I think you want to test if there is a number in the name: You could do something like this:

while True:
    user_name = str(input("Enter your full name: "))
    if [i for i in list(user_name) if i.isdigit()]:
        print("invalid input")
    else:
        break

Or maybe better check that all inputs are letters in the alphabet:

alphabet = list("abcdefghijklmnopqrstuvwxyz")

while True:
    errors = 0

    user_name = str(input("Enter your full name: "))
    for i in user_name.split(" "):
        for ii in i:
            if ii not in alphabet:
                errors += 1

    if errors:
        print("You have {} errors".format(errors))
    else:
        break
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
0

The return type of input is always an instance of str. Even if the user enters what appears to be a number the result is still a string containing the number. e.g. if they enter 5, it is still given to you as the string '5'.

So instead what you need to do is to check to see if it is an integer.

e.g.

user_name = input("Enter your full name: ")
try:
    int(user_name)
except ValueError:
    pass
else:
    print("Enter a string")

The above code first reads the input into a variable, this will always be a string. It then tries to convert it to an integer with the int() method. If that conversion fails (which is what we want). We just carry on as normal (using pass). Otherwise it will hit the else and print our your message.

0

The except-part of try-except only run if what's inside the try-part throws an error.

An example is divisibility by zero. The following code will throw an error when trying to run it in the python shell;

print(5/0)

You can catch this error, and print your own message instead of the python shell printing its own. In this case ZeroDivisionError is the certain type of error python will throw. With the following code, python will just catch that error, and not any other.

try:
     print(5/0)
except ZeroDivisionError:
     print("Cannot divide by zero")

If you want to catch all errors, you simply just write except instead of except zeroDivisionError.

The code inside the except-block don't run because there is no error when trying to run what's inside the try-block. What is happening inside the try-block is simply assigning an input to a variable. There is no error throwed for this line, and thus the except-block don't run.

There are different ways to get the functionality you want. You probably want to repeat that the input needs to be a string, until the user actually enters a string. You can do just that with a while-loop. The specific error that is thrown if string to integer conversion fails is ValueError.

isString = False
while not isString:
      userInput = input("Enter here: ")

      try:
           int(userInput)
      except ValueError:
           # if string to integer fails, the input is a string
           isString = True
      else:
            print("Please enter a string")

The while loop above runs as long as isString is False. First we try to convert from string to integer. If this throws an error, the input is a string, therefore we're setting the isString to True, and the while-loop will not run anymore. If the conversion is sucessfull, it implies that the input is actually an integer, and thus the else-statement will run, printing that the user needs to enter a string.

Markus
  • 182
  • 5