0

I am trying to ask the user for a string only but whenever the user types an integer or a float, it does not execute the except ValueError code.

Here is my code:

word = input('Enter a string.')


try:
    word1 = str(word)
    print(word1)
    print(type(word1))
except ValueError:
    print('The value you entered is not a string.')
niknik
  • 11
  • 1
    User input values queried with the `input` function are always returned as string. Also, it would be perfectly admissible to convert an integer or a float to a string. Your code simply never raises any `ValueErrors` to be caught and processed – Lukas Thaler May 18 '21 at 06:19
  • The function ```input``` return the the user input always return a string. instead of check if its a string type try convert him as float/integer. – Eitan Rosati May 18 '21 at 06:24
  • You should define precisely what charcaters you accept in a "string" as it is not precise enough. If needed you can use regex (module re) to find digits in the string and print an error message. No exception handling necessesary here, a simple if could work fine. – Malo May 18 '21 at 06:46

3 Answers3

0

The function input always returns a string. you can check if the input is a number in one of the following ways:

Use build in methods in str Class:

word = input('Enter a string.')


# Check if the input is a positive integer without try except statements:
if word.isnumeric():
    # The word contain only numbers - is an positive integer.
    print("positive int")

Try convert the variable in try expect statement:

word = input('Enter a string.')

# Check if the input is a positive integer with try except statements:
try:
    word = float(word)
    print("the input is a float")

except ValueError:
    print("the input is a string")
Eitan Rosati
  • 543
  • 5
  • 18
0
  • input() function in python always takes input as string only. So your code will never throw an exception.

    The valueError will catch the error in this case -> int(word). Where word does not contain a pure number.

  • Always include your statements in double-quotes. Eg. input("Enter the name: ")

Good, you asked this doubt. It's always good to ask for mistakes.

abdeali004
  • 463
  • 4
  • 9
  • "Always include your statements in double-quotes. Eg. input("Enter the name: ") " : why ? [it seems to be the same](https://stackoverflow.com/questions/56011/single-quotes-vs-double-quotes-in-python) – DonKnacki May 18 '21 at 07:41
  • Because it is the best practice in programming. Double quotes should be used in Strings and single in specifying characters. Eg. 'C', "Python is great." It gives more clarification and many languages did not support a single quotes in strings. – abdeali004 May 18 '21 at 14:27
-1

By defaults input function in python consider your input as "string " whatever enter code hereyou type it is string . for converting your input into integer you have to type this code .

word = int(input('Enter a string.'))

try:
    word1 = str(word)
    print(word1)
    print(type(word1))
except ValueError:
    print('The value you entered is not a string.')

In this case your code is working .

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • it executed " except valueError: " when your try block raise an error but in your case it is not raising any error . – Shubhanshu Nishad May 18 '21 at 06:47
  • The int(...) part could raise an exception, this should be protected by an exception handling. This example is not working as it is always possible to converts a number into a string without exception/error. – Malo May 18 '21 at 06:48
  • If the user will try to insert a string the program will raise an exception – Eitan Rosati May 18 '21 at 06:48