0

IDLE is registering if statements as syntax errors. My code is as follows:

  import random ;\
    print("Welcome to the fortune cookie simulator") ;\
    print("\n\nThe fortune cookie minus the good part..") ;\
    input("\n\n\nPress enter to recieve your fortune!") ;\
    fortune = random.randint(1, 5) ;\
    if fortune==1:  ;\
      print("You will die today") ;\
8
  • 3
    Why do you have ;\ at the end of every line? Commented Sep 28, 2014 at 20:50
  • This isn't python syntax... or rather, I should say, this is a one-liner. Commented Sep 28, 2014 at 20:52
  • 1
    Splitting a one-liner across multiple lines.... interesting. Commented Sep 28, 2014 at 20:53
  • How can you have an if in the middle of a one-liner when if-blocks are determined by indentation? Commented Sep 28, 2014 at 20:54
  • If the body of the if statement is itself just one line, you can write the whole thing as one line. Commented Sep 28, 2014 at 20:56

2 Answers 2

1

Since if fortune==1: is not a complete statement, you cannot terminate it with a ;. The correct one-line form of an if statement is simply

if fortune==1: print("...")

Splitting that into two lines, then, is simply using normal Python

if fortune==1:
    print("...")

Why you are trying to fit multiple statements into one logical line is quite another question.

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

2 Comments

Python is ill-suited for one-liners. Let us not encourage it?
Oh, I'm not. I'm just trying to point out how two wrongs (using ; to combine two lines, then using `` to split the line into two again) produced one bigger wrong.
0

You created one big oneliner with your explicit statement separators and line continuations. Its ugly and bug prone to work like that. I am going to assume you are working in python3. In python2, input() expects the user to enter a valid python expression. if you are using python2, use raw_input() instead.

import random
print("Welcome to the fortune cookie simulator")
print("\n\nThe fortune cookie minus the good part..")
input("\n\n\nPress enter to recieve your fortune!")
fortune = random.randint(1, 5)
if fortune == 1:
    print("You will die today")

Comments

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.