0
因此,由於某種原因,我逃過了這個代碼game_mechanics
方法似乎並沒有運行,我不知道爲什麼。 new_game()
到Number_Game()
似乎過得去,所以我不知道爲什麼其他不起作用。與類的猜測遊戲
from random import randint
class Number_Game() :
secret_num = randint(0,10)
def __init__(self):
self.user_numb_guesses = 0
self.user_play = 0
self.secret_num = randint(0,10)
self.user_act_guess = 0
def game_mechanics(self) :
self.user_act_guess = int(input("Pick a number, any whole number, between 0 and 10. Choose wisely."))
if self.user_act_guess > 10:
print ("You have chosen poorly, I said to choose a number between 1 and 10.")
self.user_act_guess = int(input("Choose again"))
self.user_numb_guesses = self.user_numb_guesses + 1
if self.user_act_guess > self.secret_num :
print ("You have chosen poorly, my number is smaller than that. You have guessed wrong %d times." % self.user_numb_guesses)
self.user_act_guess = int(input("Try again, choose a number between 1 and 10"))
self.user_numb_guesses = self.user_numb_guesses + 1
if self.user_act_guess < self.secret_num :
print ("You have chosen poorly, my number is larger than that. You have guessed wrong %d times." % self.user_numb_guesses)
self.user_act_guess = int(input("Try again, choose a number between 1 and 10"))
self.user_numb_guesses = self.user_numb_guesses + 1
if self.user_act_guess == self.secret_num :
self.user_play = int(input("Congratulations! You have chosen wisely, %d was the number I was thinking of.It only took you %d tries. Press 1 to keep playing." % (self.user_act_guess , self.user_numb_guesses)))
if self.user_play == 1 :
new_game()
def new_game():
user_play = input("Enter 1 to play the guessing game")
while user_play != "1" :
new_game()
if user_play == "1" :
Number_Game()
new_game()
歡迎來到SO。請閱讀此[如何問](http://stackoverflow.com/help/how-to-ask),並按照指導方針來改善您的問題,並提供足夠的信息來描述和重現您的問題。 – thewaywewere
** **如何不「運行」? – martineau
它詢問用戶是否想玩,然後程序結束。它並沒有轉到該類的game_mechanics()部分。 – Will