# What is wrong with this code?

+2 votes
asked May 4, 2020
colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
input ("What Element would you like to print? (Number): ")
if:
input = 0
print (colours)
else:
elif:
input = 1
print (colours)
else:
elif:
input = 2
print (colours)
else:
elif:
input = 3
print (colours)
else:
elif:
input = 4
print (colours)
else:
elif:
input =(> 4)
print ("Sorry, error.")

## 8 Answers

+4 votes
answered May 16, 2020 by (520 points)
selected May 16, 2020 by Aeshan Dave

Best answer
i thing every thing is right
commented May 18, 2020 by (100 points)
an equivalent code.

colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
num = float(input("Enter a number: "))
if num == 0:
print (colours)
elif num == 1:
print (colours)
elif num == 2:
print (colours)
elif num == 3:
print (colours)
elif num == 4:
print (colours)
else:
print ("Sorry, error.")
commented Jun 7, 2020 by (190 points)
All other answers are totally correct, just adding this assuming you have knowledge about errors which can arise.
colors = ("Red", "Orange", "Yellow", "Blue", "Purple")
try:
print(colors[int(input("What element would you like to print? (Number): "))])
except IndexError:
print("Out of Index")
commented Jan 28 by (2,860 points)
this one works
+2 votes
answered May 18, 2020 by (780 points)

try it this way:

colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
input=int(input("What Element would you like to print? (Number): "))
if input == 0:
print (colours)
elif input == 1:
print (colours)
elif input == 2:
print (colours)
elif input == 3:
print (colours)
elif input == 4:
print (colours)
elif input >= 4:
print ("Sorry, error.")

+2 votes
answered May 22, 2020 by (230 points) 1 flag
you have to use (else if) statement  instead of (if) statement
0 votes
answered May 22, 2020 by (230 points)

try it this way:

colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
input=int(input("What Element would you like to print? (Number): "))
if input == 0:
print (colours)
elif input == 1:
print (colours)
elif input == 2:
print (colours)
elif input == 3:
print (colours)
elif input == 4:
print (colours)
elif input >= 4:
print ("Sorry, error.")

commented May 22, 2020 by (170 points)
Wow, thanks!
0 votes
answered May 22, 2020 by (220 points)
Use nested if statements (if else if) in your code that will help make it easier to read and condense your code further to help you find more errors while debugging
0 votes
answered May 23, 2020 by (180 points)

you need to write elif rather than else because,if you write else, it stops there and doesn't check the next condition because it's the last condition so you should use elif in order to continue to the next condition

0 votes
answered May 23, 2020 by (290 points)
colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
input=int(input("What Element would you like to print? (Number): "))
if input == 0:
print(colours)
elif input==1:
print (colours)
elif input==2:
print (colours)
elif input==3:
print (colours)
elif input==4:
print (colours)
else:
print ("Sorry, error.")

https://onlinegdb.com/SkVrCjUiL
0 votes
answered Jun 1, 2020 by (170 points)
I don't know