+ 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')
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')
+ 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")
+ 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")
+ 1
I mean thx *guys
0
Thx giys
0
Krauz The Jester, your code will not work since you capitalized the statements and did not indent
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.
- 1
Your code has a problem with indentation