我的elif声明有什么问题?语法无效

时间:2016-01-04 15:22:06

标签: python-3.3

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")

具有语法错误的位是第1个elif语句(第2个选项)。如有必要,请忽略之前的所有代码(它适用于上下文)。每当我运行代码时,它都会说出语法错误,只是在单词elif的末尾定位输入行(我不知道它叫什么)。

2 个答案:

答案 0 :(得分:0)

这是一个简单的修复,使用if else语句需要关闭ELSE,在这种情况下,当你的程序运行时不会这样,如果没有其他的话,看到它是孤独的:)

if option == "1":

elif option == "2":

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

这里也可以使用switch语句,因此它不会继续检查每个条件,直接进入正确的情况:D

答案 1 :(得分:0)

问题是你的块与第一个if语句分开,它实际上属于它。实际上,它遵循game.mainloop()语句,并添加了意外的缩进。尝试重新排列代码,如下所示:

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 ]