因此,由于某些原因导致我失败,此代码中的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()
答案 0 :(得分:0)
from random import randint
class Number_Game() :
def __init__(self):
self.user_numb_guesses = 0
self.user_play = 0
self.secret_num = randint(0,10)
self.user_act_guess = 0
self.game_mechanics() # This is new
def game_mechanics(self) :
while(True): # This is new
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()
break
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()
我修改了几行。