语法错误:输入错误('def')

时间:2018-07-18 19:27:10

标签: python

我正在做Coursera迷你项目。

我将这个问题作为行def range100()的错误输入('def')。我正在尝试在范围内玩猜数字游戏。我检查了缩进就可以了。请帮忙。谢谢

import simplegui
import random
import math

# helper function to start and restart the game
def new_game():
    # initialize global variables used in your code here
    global secret_number
    low = 0
    global n
    if range100():
        high = math.ceil(100)
        n = int(math.log((high - low + 1),2))
    elif range1000():
        high = math.ceil(1000)
        n = int(math.log((high - low + 1),2)



# define event handlers for control panel
def range100():
    # button that changes the range to [0,100) and starts a new game 
    global secret_number
    secret_number = random.randrange(0, 100)
    return True


def range1000():
    # button that changes the range to [0,1000) and starts a new game     
    global secret_number
    secret_number = random.randrange(0, 1000)
    return True


def input_guess(guess):
    # main game logic goes here 
    global secret_number
    guess_num = int(guess)
    print 'Guess was %s' %guess_num
    global n
    if secret_number > guess_num:
        print 'Lower'
        n -= 1
        if n >= 0:
            print 'Number of remaining guesses is %s' %n
        else:
            print 'You are running out of chances'
            new_game()
    elif secret_number < guess_num:
        print 'Higher'
        n -= 1
        if n >= 0:
            print 'Number of remaining guesses is %s' %n
        else:
            print 'You are running out of chances'
            new_game()
    else:
        print 'Correct'



# create frame
frame = simplegui.create_frame('Guess the number',200, 200)
frame.add_input('Input Guess:', input_guess, 100)
frame.add_button('Range is [0, 100)', range100, 100)
frame.add_button('Range is [0, 1000)', range1000, 100)

# register event handlers for control elements and start frame
frame.start()

# call new_game 
new_game()

我正在尝试计算猜测并输出结果。但是我无法通过此运行,因为我不知道def为什么语法错误。...

1 个答案:

答案 0 :(得分:1)

查看您的代码,您在bazel-bin/test之前的行中缺少括号。该行应为def range100()