Python - Ako zistiť, či vstup užívateľa je Komplexná typ vstupné

0

Otázka

som chcete vytlačiť správu, v závislosti od typu vstupnej ale zakaždým, keď som vstupné komplexné číslo, NAPRÍKLAD (5j) je detekovaný, ako je reťazec textu. Ako mám vyriešiť to, prosím? Vďaka.

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')

Príklad realizácie:

a ? 5j
please input a int or float
Valid number, please not String!

snažil som sa robiť 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 to nie je "Dokonalý"

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

Môžete pridať prvý blok kódu do skúste blok

Ako je tento -

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')

Toto je to, čo ste mali na mysli?

2021-11-24 04:15:41

Nie, bohužiaľ je to stále vypisuje "String chyba" keď inputing komplexná číslo
Medin Oari
0

Môžete použiť vnorené try-except a vstavanú funkciu complex() namiesto toho.
Takže, váš kód musí byť ako tento

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 iných jazykoch

Táto stránka je v iných jazykoch

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