Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What is wrong with this code?

+2 votes
asked May 4, 2020 by Aeshan Dave (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.")

8 Answers

+4 votes
answered May 16, 2020 by HinaMuneer (520 points)
selected May 16, 2020 by Aeshan Dave
 
Best answer
i thing every thing is right
commented May 18, 2020 by Munwar V.H (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.")
commented Jun 7, 2020 by Tushar Goswami (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 Jeff The Chicken (2,810 points)
this one works
+2 votes
answered May 18, 2020 by bhoomi2000 (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.")

+2 votes
answered May 22, 2020 by Mohammed Nadeem (230 points) 1 flag
you have to use (else if) statement  instead of (if) statement
0 votes
answered May 22, 2020 by Mohammed Nadeem (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[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, 2020 by Aeshan Dave (170 points)
Wow, thanks!
0 votes
answered May 22, 2020 by CHRISTOPHER STUART (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 ARAVIND (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 dinesh sen (290 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
0 votes
answered Jun 1, 2020 by NG ZHI YOUNG (170 points)
I don't know
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.
...