Python - Jak zkontrolovat, zda vstup uživatele je Komplexní typ vstupní

0

Otázka

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í"

complex-numbers input python string
2021-11-24 03:53:28
2
0

Můžete přidat první blok kódu do try bloku

Takhle -

while True:
    try:
        a = input("a ? ")
        if (isinstance(a, complex)):
            print("Valid number, please not Complex!")  
        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')

Je to to, co jsi měl na mysli?

2021-11-24 04:15:41

Ne, bohužel to stále tiskne Řetězec "error", když inputing komplexní číslo
Medin Oari
0

Můžete použít vnořené try-except a vestavěné funkce complex() místo.
Takže, váš kód musí být takhle

while True:
    a = input("a? ")
    try:
        a = float(a)
        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')
    except ValueError:
        try:
            a = complex(a)
            print("Valid number, please not Complex!")
        except ValueError:
            print ("Valid number, please not String!")
            continue
2021-11-24 05:29:19

V jiných jazycích

Tato stránka je v jiných jazycích

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................