![]() |
Whats wrong with the elif? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Whats wrong with the elif? (/thread-31237.html) |
Whats wrong with the elif? - inunanimous93 - Nov-29-2020 >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: x = 0 print('Negative changed to zero') >>> elif x == 0: SyntaxError: invalid syntax >>> I was trying to run through the Python tutorial. This is 4.1 RE: Whats wrong with the elif? - Axel_Erfurt - Nov-29-2020 it's not complete, elif x == 0: then what ? x = int(input("Please enter an integer: ")) if x < 0: x = 0 print('Negative changed to zero', x) elif x == 0: print(x) else: print(x) RE: Whats wrong with the elif? - bowlofred - Nov-30-2020 You are trying to do an if/elif in interactive mode. This is a bit tricky. You need to complete your if/elif/else before getting back to the >>> prompt. If you're back that far, the if section is complete and you can't start with elif, but only with a new if.Putting all this into a file and executing the file is much easier. But here is a similar thing in interactive mode. Note that after the print, I immediately give the (unindented) elif and don't get back to the ">>>" prompt. >>> x = int(input("Please enter an integer: ")) Please enter an integer: 42 >>> if x < 0: ... x = 0 ... print("Negative changed to zero") ... elif x == 0: ... print("Don't enter zero") ... else: ... print(f"Your positive integer was {x}") ... Your positive integer was 42 RE: Whats wrong with the elif? - deanhystad - Nov-30-2020 Unless I am debugging or experimenting I don't enter code at the Python prompt. Write code in a file (i.e. myprogram.py) and run the file "python myprogram.py". |