0

I want to make this program print "Only str type supported" when 'name' gets any data type that isn't str. What should i change in this code?

while True:
name =(input("Whats ur name?"))
if name == 'Richard':
    print('ok')
    break
elif name == "gosha":
    print('FEUER FREI! "BANG BANG BANG BANG"')
else:
    print('denied')

4 Answers4

0

You can branch on the type of name such as:

if type(name) is not str:
    print('Only str type supported')

or

if not isinstance(name, str):
    print('Only str type supported')

But as others have pointed out, input() will always return a str.
https://docs.python.org/3/library/functions.html#input

Mark McElroy
  • 373
  • 4
  • 8
  • `type(some_variable)` is suboptimal, always use `isinstance`. see: https://stackoverflow.com/questions/1549801/what-are-the-differences-between-type-and-isinstance – IllustriousMagenta Jul 02 '20 at 06:26
0

For what i know, all input is a string, but I understand that what you want is to only receive letters and no numbers in the input. In that case, you can check each element of the string to see if they are numbers, and if one is found, return the error message.

To check it you would have to use:

for c in name:
    if c.isdigit():
        print("Only str type supported")
        break

This will iterate through the input string and check if any of the elements on it is a number.

EDIT: you could just use name.isalpha() to check if all the characters are letters. Would be easier to read and eliminates the unnecessary loop.

Alberefe
  • 27
  • 5
0
while True:
    name =(input("Whats  ur name?"))
    check = True
    for ele in name:
        try:
            fl = float(ele) 
            nu = int(ele)
            print("Only str type supported")
            check = False
        except:
            pass
    if check:
        if name == 'Richard':
            print('ok')
            break
        elif name == "gosha":
            print('FEUER FREI! "BANG BANG BANG BANG"')
        else:
            print('denied')

It checks individual characters of the name and tries to convert them to float or int. If a value can be converted, it fails, throws the error and asks for name again

-1

Your current question is hard to answer. The input function will always return a string. That string might be a "1" or at "3.14159" or empty "" but it will always be a string.

When you say you want it to only support string types, maybe you mean you want to check if the given input can be converted to an int, a float, or is empty?

If that's the case use try and except and rely on the cast to int or float failing to "prove" it's a string by eliminating it's ability to be a different type.

Add additional type checks as needed to make your logic more robust.

Do something like this:

while True:
    name = input("Whats ur name? ")
    try:
        float(name)
        int(name)
        print("I only support strings that are names")
        break
    except ValueError:
        pass
    print("It's not a float or an int so it's a string!")
Josh R
  • 1,970
  • 3
  • 27
  • 45