+ 2

What is wrong with my code

x = int(input('choose a number from 1 to 3 ')) if x ==1: print ('1 oh cool') else: if x==2: print('2 oh cool') else: if x==3: print('3 oh cool')

11th Jun 2021, 5:21 PM
Mr.beans
Mr.beans - avatar
9 Antworten
+ 4
You wrote the second "else" inside the condition "if x==2:" x = int(input('choose a number from 1 to 3')) if x ==1: print (': 1 oh cool') else: if x==2: print(': 2 oh cool') else: #debug if x==3: print(': 3 oh cool')
11th Jun 2021, 7:25 PM
Solo
Solo - avatar
+ 3
nums ={1:"1 oh cool",2:"2 oh cool", 3:"3 oh cool"} x = int(input()) if x in nums: print(nums[x]) else: print("wrong input")
11th Jun 2021, 6:34 PM
Shadoff
Shadoff - avatar
+ 2
also if you use python version >=3.10 then you can use match-case(switch-case in other languages): x = int(input()) match x: case 1: print ("1 oh cool") case 2: print("2 oh cool") case 3: print("3 oh cool") case _: print("wrong input")
12th Jun 2021, 4:36 AM
Shadoff
Shadoff - avatar
+ 1
I mean thx *guys
11th Jun 2021, 10:36 PM
Mr.beans
Mr.beans - avatar
0
Thx giys
11th Jun 2021, 10:36 PM
Mr.beans
Mr.beans - avatar
0
Krauz The Jester, your code will not work since you capitalized the statements and did not indent
11th Jun 2021, 11:43 PM
Solo
Solo - avatar
0
Pariket, it is recommended but not necessary. It just clarifies code, there is no other difference (could also be that the `elif` variant is interpreted faster). The real problem is what Vasiliy said.
13th Jun 2021, 4:20 AM
#0009e7 [get]
#0009e7 [get] - avatar
- 1
Your code has a problem with indentation
13th Jun 2021, 8:02 AM
Abhinandan Prasad