-1
if option == "1":
    with open("sample.txt","r") as f:
        print(f.read())

    numbers = []

    with open("sample2.txt","r") as f:
        for i in range(9):
            numbers.append(f.readline().strip())
        print(numbers)

        from random import randint

    for i in range(9):
        print(numbers[randint(0,8)])

from tkinter import *

def mhello():
    mtext = ment.get()
    mLabel2 = Label(test, text=mtext).pack()
    return

test = Tk()
ment = StringVar()
test.geometry('450x450+500+10')
test.title('Test')
mlabel = Label(test, text='Time to guess').pack()         
mbutton = Button(test, text='Click', command = mhello).pack()         
mEntry = Entry(test, textvariable=ment).pack()
test.mainloop()

from tkinter import *
def mhello():
    my_word = 'HELLO'
    mtext = ment.get()
    if my_word == mtext:
        mLabel2 = Label(test, text='Correct').pack()

    else:
        mLabel2 = Label(test, text='Incorrect').pack()
    return
test = Tk()
ment = StringVar()
test.geometry('450x450+500+300')
test.title('Test')
def label_1():
label_1 = Label(test, text='Hello. Welcome to my game.').pack()
def label_2():
    label_2 = Label(test, text='What word am I thinking of?').pack()
    button_1 = Button(test, text='Click', command = mhello).pack()
    entry_1 = Entry(test, textvariable=ment).pack()
label_1()
test.after(5000, label_2)
test.mainloop()
from tkinter import *
from random import shuffle
game = Tk()
game.geometry('200x200')
game.grid()
game.title("My Game")
board = [1, 2, 3, 4, 5, 6, 7, 8, 9]
def board_1():
    board1 = []
    k = 0
    for i in range(3):
        for j in range(3):
            board1.append(Label(game, text = board[k]))
            board1[k].grid(row = i, column = j)
            k +=1
def board_2():
    shuffle(board)
    board2 = []
    k = 0
        for i in range(3):
        for j in range(3):
            board2.append(Label(game, text = board[k]))
            board2[k].grid(row = i, column = j)
            k +=1
board_1()
game.after(5000, board_2)
game.mainloop()

#2nd Option
    elif option == "2":
        print ("You have chosen option 2. Well Done, You Can Type! XD")

The bit that has the Syntax Error is the 1st elif statement (2nd Option). Ignore all the code prior to this if necessary (it is there for context). Whenever I run the code it says that there is a syntax error and just positions the typing line (I don't know what it's called) at the end of the word elif.

2 Answers2

0

This is a simple fix, with if else statements you need to have a closing ELSE and in this case there is not so when your program runs it sees that theres a lonely if without its else :)

if option == "1":

elif option == "2":

else:
    'do something else in the program if any other value was recieved'

also a switch statement can be used here so it does not keep checking each condition and just goes straight to the correct case :D

ImDeveloping
  • 101
  • 9
  • Thank you for the reply, but I do already have an else statement in my actual code, I just could not put it in as there was already too much code in the question – Adam fake Jan 11 '16 at 15:21
  • Not a problem! did you refer to andreas-hofmann answer? what is the update on this question have you solved your issue? – ImDeveloping Jan 12 '16 at 17:34
0

The problem is that your block is separated from the first if-statement, where it actually belongs to. As it is, it follows the game.mainloop() statement, and adds an unexpected indentation. Try to rearrange your code like so:

if option == "1":
    with open("sample.txt","r") as f:
        print(f.read())

    numbers = []

    with open("sample2.txt","r") as f:
        for i in range(9):
            numbers.append(f.readline().strip())
        print(numbers)

        from random import randint

    for i in range(9):
        print(numbers[randint(0,8)])
elif option == "2":
    print ("You have chosen option 2. Well Done, You Can Type! XD")

[ Rest of the code ]
andreas-hofmann
  • 2,378
  • 11
  • 15