2 Replies - 436 Views - Last Post: 11 October 2017 - 03:33 AM Rate Topic: -----

#1 wut_wut_wut  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-October 17

Else is returning an invalid syntax error

Posted 10 October 2017 - 05:11 PM

Hi, I'm having a problem with the else function. This is the entire code.
print "********************************"
print "* Welcome to the guessing game *"
print "********************************"
from random import randint
randnum = randint(0, 100)
congrats = "Congratulations, you won!"
oops = "No"
high = "Too high"
low = "Too low"
loop = 10
loop1 = 10
loop2 = 15
diff = input("% Difficulty? % ")
allloop = 0
if diff == 0:
  while loop > 0:
    guess = int(input("# Enter a number 0 - 100 # "))
    if guess == randnum :
      print "# " + congrats + " #"
      loop = loop - 15
    elif guess > randnum:
      print "# " + high + " #"
      print "# " + oops + " #"
      loop = loop - 1
      print "# " + str(loop) + " tries left. #"
    elif guess < randnum:
      print "# " + low + " #"
      print "# " + oops + " #"
      loop = loop - 1
      print "# " + str(loop) + " tries left. #"
    else :
      print "# nope #"
elif diff == 1:
  while loop1 > 0:
    guess = int(input("* Enter a number 0 - 100 * "))
    if guess == randnum:
      print "* " + congrats + " *"
      loop1 = loop1 - 10
    elif guess != randnum:
      print "* " + oops + " *"
      loop1 = loop1 - 1
      print "* " + str(loop1) + " tries left. *"
    else :
      print "* nope *"
else :
  print "no"
print "% " + str(randnum) + " was the random number. %"


The problem is with the last part.
else :
  print "no"


It gives me the error "invalid syntax" and nothing else. What did I do wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Else is returning an invalid syntax error

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Else is returning an invalid syntax error

Posted 10 October 2017 - 07:12 PM

When it isn't obvious in Python, check your spacing. Are you using a tab when it should have been spaces? Do you have the correct number of spaces? Python is picky about whitespace. I copied your code and ran it virtually without a problem other than needing to make sure that you read in an integer. But I got it to print "no" as expected. So my guess is your have a spacing problem.
Was This Post Helpful? 0
  • +
  • -

#3 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 322
  • View blog
  • Posts: 1,060
  • Joined: 27-December 13

Re: Else is returning an invalid syntax error

Posted 11 October 2017 - 03:33 AM

I agree to Martyr2s comment and will add:

Why do you use Python 2 ? If you are beginner in Python I would recommend Python 3.

You have a lot of (semi-)redundant code. Try minimizing redundancy by use of functions and/or optimized code structure. It is important to develop good coding habits, even in a small program like this.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1