(Sorry in advance if there's a solution I'm maladroitly overlooking.)
Recently I've stumbled upon something that seems somewhat simple, but I can't figure it out. It goes as follows: If I get an input that has been converted to an int once I initialize the value receiving it (Ex. number = int(input())). Then I go on to set up an if statement that has conditions that compared if the input equals a number, is there also a way I can set a condition that compares if the number is equal to a string?
An example of a small test script I created to elucidate on this problem further:
numb_var = int(input("Test "))
if numb_var == 1 or numb_var == **str("test.")**
print("Success")
else:
print("Failure")
The bolded portion is something I tried, which ultimately lead to an error I'll get to later.
I tried using a few things:
== str(test)
== "tes"
str(numb_var) == "test"
All to no avail.
The errors just all follow "Invalid Syntax"
Is it truly impossible to do this?
Thanks, Prior-Maxim.
You can do something like this:
var = input("Test ")
numb_var = None
try:
numb_var = int(var)
except ValueError as e:
print('cannot be converted to an int ', e)
if numb_var == 1:
print("Success")
elif var == 'test':
print('Its a string')
else:
print("Failure")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.