0

I'll try to make a python program, convert roman to number. I believe my logic for the program is correct, but I get syntax error. maybe someone want's help me to fixed my program.

this my whole program:

bil = int(input())
if(bil<1 or bil>99):
    print("no more than 3999")
else:
    while(bil>=1000):
        print("M")
        bil-=1000
        if(bil>=500):
            elif(bil>500):
                elif(bil>=900):
                    print("CM")
                    bil-=900
    else:
        print("D")
    while(bil>=100):
        if(bil>=400):
            print("CD")
            bil-400
        else:
            bil-=100
        if(bil>=50):
            elif(bil>=90):
                print("XC")
                bil-=90
        else:
            print("L")
            bil-=50
    while(bil>=10):
        if(bil>=40):
            print("XL")
            bil-=40
        else:
            print("X")
            bil-=10
        if(bil>=5):
            elif(bil==9)
            print("IX")
            bil-=9
        else:
            print("V")
            bil-=5
    while(bil>=1):
        if(bil==4):
            print("V")
            bil-=4
        else:
            print("I")
            bil-=1

I got syntax error in line :

elif(bil>500):

I need your opinion, thank you.

8
  • 3
    So what does the elif bil>500: mean to you? Have you looked up the if/elif/else construct in python? Commented Mar 13, 2021 at 15:32
  • I can’t find this syntax for elif in the Python docs - unclear as to why you would expect it to work as-is. Commented Mar 13, 2021 at 15:33
  • i try to put the same logic in c language. Commented Mar 13, 2021 at 15:57
  • So would you also use the indentation you have shown when writing in c? Commented Mar 13, 2021 at 15:59
  • I think I've adjusted in python language Commented Mar 13, 2021 at 16:00

1 Answer 1

4

It shouldn't be elif, it should if bil>500. Because, you are trying to create a nested if condition and not an if/elif/else condition. So the final code in that block should be:

if(bil>=500):
    if(bil>500):
        if(bil>=900):
            print("CM")
            bil-=900

Also, I don't understand why you are comparing bil>500 two times at the same time. You could remove one if statement there And there are many such if/elif blocks out there. You need to replace elif with if, if there is an indentation or you need to remove indentation for elif condition and write a condition for the if block too

Sign up to request clarification or add additional context in comments.

10 Comments

i'll try to change it to if but my program unable to show the expected results
are you getting any error, or not getting the expected output
i got expected output
So, is the program working correctly now?
I'll try my program using if and else if in c program before and succes, and i got the right result, then i change to python and then i got error syntax
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.