[英]Print statements not working in if/elif/else block
我有一個我要重做的項目。 這是一門有關交互式Python編程的課程。 提交的截止日期已經過去,所以我在這里沒有違反榮譽代碼,只是想了解為什么我現在為同一項目使用的代碼無法正常工作。
游戲是猜數字。 if塊中的所有print語句都根本不會打印出來,我當然認為應該打印出來。 在評估階段,我已經完成了其他幾個學生項目,但是仍然看不到我的代碼為什么不正確。
游戲在瀏覽器中執行此游戲在瀏覽器中點擊這里
這是我正在使用的代碼:
# Sanderson, Steven
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
import simplegui
import random
import math
# initialize global variables used in your code
secret = 0 # The secret number
guesses = 0 # The amount of guesses you will have left
guess_count = 0 # How many guesses you made
range_limit = 100 # The number range of the game
def init():
# Here I reference the global variables because they
# are now inside of a function being referenced locally
global range_limit
global secret
# This following line limits the amount of guesses
# the player can have based upon the given
# range_limit of the game
guesses = int(math.ceil(math.log(range_limit,2)))
# This following line of code generates a secret
# number for the specified range of the game,
# it has been initialized to a range of 100 to
# start the game
secret = random.randrange(0, range_limit)
# Converts range_limit to a string to print to console
print "Play Guess The Number. The range is 0 to", + range_limit
print "You have " +str(guesses) + " guesses for this game"
print ""
# define event handlers for control panel
def range100():
# button that changes range to range [0,100) and restarts
global range_limit
global secret
global guesses
range_limit = 100
init()
def range1000():
# button that changes range to range [0,1000) and restarts
global range_limit, secret, guesses
range_limit = 1000
init()
def input_guess(guess):
# main game logic goes here
int_guess = int(guess)
global guess_count
global guesses
global secret
guesses += -1
guess_count = guess_count + 1
if int_guess == secret:
print "You Win and did it in " +str(guess_count) + " guesses"
print ""
init()
elif int_guess > secret:
print "You guessed " +str(guess) + " guess lower"
print "You have " +str(guesses) + " guesses left"
print ""
elif int_guess < secret:
print "You guessed " +str(guess) + " guess higher"
print "You have " +str(guesses) + " guesses left"
print ""
if guesses == 0:
print "You ran out of guesses, the secret was " +str(secret)
print ""
init()
# create frame
frame = simplegui.create_frame("Guess The Number!", 200, 200)
# register event handlers for control elements
frame.add_button("Range [0, 100)", range100, 125)
frame.add_button("Range [0, 1000)", range1000, 125)
frame.add_input("Enter your guess here and press enter", input_guess, 125)
# start frame
frame.start()
init()
# I know my code is not the best, constructive criticisim in order to help me
# be a better coder is appreciated :)
這是我剛運行的示例輸出。 我guess_count
順序打印出secret
, guess_count
和guesses
。
71
0
0
You guessed 25 guess higher
You have -1 guesses left
71
1
-1
You guessed 25 guess higher
You have -2 guesses left
71
2
-2
You guessed 25 guess higher
You have -3 guesses left
當按下按鈕時,由於未重置計數,看起來init()也不起作用,這會刮傷頭部。
如果添加行
print repr(guess), type(guess), repr(secret), type(secret)
print guess == secret, guess > secret, guess < secret
就在if
分支之前,您會看到(類似):
'23' <class 'str'> 4 <class 'int'>
False False False
您正在將字符串與整數進行比較,這在Python 3中會給出TypeError: unorderable types: str() < int()
但在Python 2中,它將無提示地返回意外行為(如果有內存,則基於類的名稱,但是因為我們無論如何都不應該這樣做,所以我永遠都不會記住細節。)要具體:
>>> "47" < 200
False
>>> "47" < 47
False
>>> "47" < 2
False
>>> 100 < "23"
True
>>> 100 < "1"
True
>>> 100 < "-23"
True
將用戶輸入轉換為整數,然后就可以開始處理下一個問題了:^)
You guessed 5
5 <class 'int'> 35 <class 'int'>
False False True
You guessed 5 guess higher
You have -1 left
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.