What is wrong with this code?

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

7 Answers

+3 votes
answered May 16 by (500 points)
selected May 16 by
 
Best answer
i thing every thing is right
commented May 18 by (100 points)
an equivalent code.

colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
num = float(input("Enter a number: "))
if num == 0:  
    print (colours[0])
elif num == 1:
    print (colours[1])
elif num == 2:
    print (colours[2])
elif num == 3:
    print (colours[3])
elif num == 4:
    print (colours[4])
else:
    print ("Sorry, error.")
+2 votes
answered May 18 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[0])
elif input == 1:
    print (colours[1])
elif input == 2:
    print (colours[2])
elif input == 3:
    print (colours[3])
elif input == 4:
    print (colours[4])
elif input >= 4:
    print ("Sorry, error.")

+1 vote
answered May 22 by (210 points)
you have to use (else if) statement  instead of (if) statement
0 votes
answered May 22 by (210 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[0])
elif input == 1:
    print (colours[1])
elif input == 2:
    print (colours[2])
elif input == 3:
    print (colours[3])
elif input == 4:
    print (colours[4])
elif input >= 4:
    print ("Sorry, error.")

commented May 22 by (170 points)
Wow, thanks!
0 votes
answered May 22 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 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 by (270 points)
colours = ("Red", "Orange", "Yellow", "Blue", "Purple")
input=int(input("What Element would you like to print? (Number): "))
if input == 0:
    print(colours[0])
elif input==1:
    print (colours[1])
elif input==2:
    print (colours[2])
elif input==3:
    print (colours[3])
elif input==4:
    print (colours[4])
else:
    print ("Sorry, error.")

https://onlinegdb.com/SkVrCjUiL
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...