mardi 29 décembre 2015

Python If(and) statement syntax error

I'm trying to make a simple console game in Python 2.7, where the user gets a dilemma and makes a decision by typing the number correlating to the option he wants to choose. This question is the main menu.

#imports
import os
import time

#classes
class question:
  def __init__(self, valid, alt):
    self.valid = valid
    self.alt = alt

#functions
def is_number(s):
  try:
    int(s)
    return True
  except ValueError:
    return False

def asker(question):
  #make it read out the question
  print(question.alt)
  #make it find the possible answers
  print(question.valid)
  #make it get the answer from the user and return it
  valid = False
  while(valid == False):
    q = question.alt
    v = question.valid
    s = raw_input("Enter a number in the range 1 to %s\n> " % v
    if (is_number(s) and s in q):           
        return s
        break
    elif (is_number(s) == False):
        print("Only numbers allowed.")
    else:
        print("That is not a valid number.")

menu =  question(["1: Start new game", "2: Load new game", "3: Settings"], 3)
asker(menu)

print("Congratulations on submitting a valid answer!")


raw_input("> ")

I'm getting a "SyntaxError: Invalid syntax" from the if (is_number(s) and s in q) statemet when trying to run the program. It was working fine before i added the question.

This is my first real program.

Aucun commentaire:

Enregistrer un commentaire