Does anyone know how to restrict my input values to only strings without using the str() function?
I am trying to restrict the input to only strings using the while statement
Why would you want to disallow numbers? What if the user’s sentence contains a number?
That’s a good point, I did not think about that.
Is there a way to still do it even if the sentence includes a number?
you just don’t want to see a number ever? Try using not my_input.isDigit()
you mean isdigit()
btw str.isdigit()
returns False
if there’s any non-digit characters in the string
also btw @jonathanessombe input
always returns a string even when the user just inputs a number
Does .isdigit accept an arguement? if it does what does it do?
No, str.isdigit()
does not accept any arguments (what would be the point of them?)
lol, I thought I solved the problem, i guess i didn’t.
This is what i have so far
Well you’re testing if string_length
is equal to True
/False
, but you either return 'please enter a valid string!'
or the length of the given string.
thank you, i just realized the mistake
"hi 98".isdigit()
is False because not all characters in it are digits.
I don’t know if i need seperate post about it but is there a way to differentiate between a string containing a digit vs just a string?
def has_digit(string):
for char in string:
if char.isdigit():
return True
return False
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.