SyntaxError:输入错误('def')

2024-09-28 22:26:05 发布

您现在位置: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语法错误。。。。


Tags: andthegamenumbernewsecretifdef