chci vytisknout zprávu v závislosti na typu vstupního ale pokaždé, když jsem vstupní komplexní číslo, NAPŘÍKLAD (5j) to je detekován jako vstupní řetězec. Jak to mám vyřešit prosím? Díky.
while True:
a = input("a ? ")
if (isinstance(a, complex)):
print("Valid number, please not Complex!")
try:
a = float(a)
except ValueError:
print ('please input a int or float')
if (type(a)==str):
print("Valid number, please not String!")
continue
if 0.5 <= a <= 100:
break
elif 0 <= a < 0.5:
print ('bigger number, please: 0.5-100')
elif a < 0:
print ('positive number, please')
elif a > 100:
print ('smaller number, please: 0.5-100')
Příklad provedení:
a ? 5j
please input a int or float
Valid number, please not String!
snažil jsem se dělat toto :
while True:
try:
a = input("a ? ")
if ('j' in a):
print("Valid number, please not Complex!")
a = float(a)
except ValueError:
print ('please input a int or float')
if (type(a)==str and 'j' not in a):
print("Valid number, please not String!")
continue
if 0.5 <= a <= 100:
break
elif 0 <= a < 0.5:
print ('bigger number, please: 0.5-100')
elif a < 0:
print ('positive number, please')
elif a > 100:
print ('smaller number, please: 0.5-100')
ale není to "Ideální"