Why else did not work | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Why else did not work

#جواب به توان n عدد مختلط (i) h = input() d = h % 4 if d == 0 : print ("1") elif d == 1 : print ("i") elif d == 2 : print ("-1") elif d == 3 : print ("-i") else : print ("undefined")

20th Feb 2023, 1:49 PM
Amir Ghannad
8 ответов
+ 2
When you input a word of character then int(h) will raise error. If you put any valid number then d value can be 0,1,2,3 only. No other values possible so else part is no use.. You may trying to exception handling when input is not valid number? Then try : h = input() try : d = int(h) % 4 if d == 0 : print ("1") elif d == 1 : print ("i") elif d == 2 : print ("-1") elif d == 3 : print ("-i") except : print ("undefined")
20th Feb 2023, 3:00 PM
Jayakrishna 🇮🇳
+ 7
AmIraN gh , we still lack a useful task description. > can you please tell us what your code should achieve?
20th Feb 2023, 7:57 PM
Lothar
Lothar - avatar
+ 2
input() accept as string format. Convert to needed type. Otherwise h%4 is error. h = int( input() )
20th Feb 2023, 1:54 PM
Jayakrishna 🇮🇳
+ 1
You only need to handle any letter before entering the if else statments try: h = int(input()) except ValueError: print("Undefined") d = h % 4 if d == 0 : print ("1") elif d == 1 : print ("i") elif d == 2 : print ("-1") elif d == 3 : print ("-i")
20th Feb 2023, 2:03 PM
Mohamed Youssef Belloui
Mohamed Youssef Belloui - avatar
+ 1
Jayakrishna 🇮🇳 and Mohamed Youssef Belloui I'm sorry what about this coad Why else did not work about words and sentences h = input() d = int(h) % 4 if d == 0 : print ("1") elif d == 1 : print ("i") elif d == 2 : print ("-1") elif d == 3 : print ("-i") else : print ("undefined")
20th Feb 2023, 2:39 PM
Amir Ghannad
+ 1
Jayakrishna 🇮🇳 thank you so much
20th Feb 2023, 3:04 PM
Amir Ghannad
+ 1
Lothar In math there is a case about complex numbers. So I create this coad to find the answer of complex numbers power
20th Feb 2023, 9:26 PM
Amir Ghannad
0
I want to print Undefined when the user prints word in the input
20th Feb 2023, 2:44 PM
Amir Ghannad