我刚刚介绍了Python但我现在仍然坚持使用我的代码,我无法让每个游戏的平均猜测游戏发挥作用。如果我能从你们那里得到一些帮助,那就太棒了!如果有的话,我也很想听到任何建设性的反馈意见。谢谢!!
# A robust number-guessing game with hinting.
import random
total_guess = 0
num_games = 0
num_guesses = 0
print("Welcome to Guessing Game!")
print()
reply = input("Are you ready to play (Y or N)?").upper()
while reply != "Y" and reply != "N":
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
while reply == "Y":
# pick a random number from 0 to 100 (both inclusive)
# There is no error in the line below, which is
# equivalent to
# number = random.randint(0, 100)
number = random.randrange(0, 101)
# make it different from number so that it goes into loop
guess = number + 1
while guess != number :
guess = int(input("Your guess (0 - 100)? "))
num_guesses += 1
if guess < number :
print("Your guess is too low")
elif guess > number :
print("Your guess is too high")
else:
print("Bingo!")
# collect the statistics to calculate average guess per game
total_guess += num_guesses
num_games += 1
print("You got it right in " + str(num_guesses) + " tries.")
num_guesses = 0
print()
reply = input("Another game (Y or N)? ").upper()
while reply != "Y" and reply != "N":
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
print("Your average guess per game is", total_guess/num_games)
答案 0 :(得分:0)
您的代码没有正确缩进。
import random
total_guess = 0
num_games = 0
num_guesses = 0
print("Welcome to Guessing Game!")
print()
reply = input("Are you ready to play (Y or N)?").upper()
while reply != "Y" and reply != "N":
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
while reply == "Y":
number = random.randrange(0, 11)
# make it different from number so that it goes into loop
guess = number + 1
while guess != number :
guess = int(input("Your guess (0 - 100)? "))
num_guesses += 1
if guess < number :
print("Your guess is too low")
elif guess > number :
print("Your guess is too high")
else:
print("Bingo!")
# collect the statistics to calculate average guess per game
total_guess += num_guesses
num_games += 1
print("You got it right in " + str(num_guesses) + " tries.")
num_guesses = 0
print()
reply = input("Another game (Y or N)? ").upper()
while reply != "Y" and reply != "N":
print("Invalid response")
reply = input("Another game (Y or N)? ").upper()
print("Your average guess per game is", total_guess/num_games)