TL:DR尝试建立一个评分系统,当你得到答案时它似乎没有上升
正如标题所说我正在自学python,因此这只是我编写的第二个代码(暗示我为什么学习python)。我非常乐意接受批评,从语法到如何更好地评论。话虽如此,让我们谈谈我的问题。
这是一款小型猜谜游戏。我正在阅读的这本书讲授了“for guessTaken”和随后的代码。我的问题在于我的代码的一个小方面。评分系统不会增加。
我在for循环中设置了名为games的代码,然后尝试在每轮开始时显示它,然后进行每次正确的猜测。但是,它会在前几轮显示0,然后它会显示2(或者我认为当前的分数是......)。我认为问题是我在if语句中将得分+1调用为int但是我已经移动了代码并且无法弄明白。
我知道它不漂亮!还有一些错误(玩的游戏数量不是你输入的数量。)现在我只是在评分系统上工作。
#this is a guess the number game.
import random
#Get's user's name
print("Hello. Welcome to Print's guessing game. Please enter your name: ")
userName = input()
#askes if they are ready
print("Are you ready to play " + userName + "?")
ready = input().lower()
if ready == 'yes' :
print("Let's get started")
else:
while ready != 'yes':
print("Let me know when you are ready")
ready = input().lower()
#Game start
#number of games
games = int(input("How many games would you like to play?"))
if games >= 1:
print("Let's get started")
for games in range (int(games), 1, -1):
while games != 1:
print("I am thinking of a number between 1 and 20 ")
secretNumber = random.randint(1, 20)
score = 0
print("Current score: " + str(score))
print("Debug: " + str(secretNumber))
for guessTaken in range (7, 1, -1):
print("you have " + str(guessTaken - 1 ) + " guesses left")
guess = int(input())
if guess < secretNumber:
print("Your guess is to low. Please guess again")
elif guess > secretNumber:
print("Your guess is too high. Maybe something a little lower?")
else:
break # This conditon is for the right guess.
if guess == secretNumber:
print("good Job " + userName + "! you guessed the number right!")
score = int(score + 1)
print("your score is " + str(score))
games = int(games - 1)
else:
print("Nope, the number I was thinking of was " + str(secretNumber))
print("you have " + str(games) + " games left")
elif games == 0:
while games == 0:
print("Would you like to exit? Yes or No ")
exit = input().lower()
if exit == 'yes':
quit()
else:
games = int(input("How many games would you like to play?"))
else:
print("wtf")
答案 0 :(得分:3)
每次代码经过while循环时,您的得分变量都会被初始化为零。如果您希望跟踪所有游戏的总分,请在print("Let's get started")
之后立即初始化。只要他们告诉你他们想要玩多少游戏,这将重置他们的分数。
如果您不希望他们的分数在您退出程序之前重置,则必须在程序开始时对其进行初始化。通过import random
向上运行会很好。