[英]I can't figure out how to save the score on this dice game I've made. What should I change?
I am so close to finishing this simple game but I cant for the life of me figure out how to save the score with the def function.我非常接近完成这个简单的游戏,但我一生都无法弄清楚如何使用 def function 保存分数。 As every time it rolls it resets to 0 due to the score = 0 in the def.
每次滚动时,由于 def 中的 score = 0,它会重置为 0。 I tried to take it out, but it kept saying that it couldn't find the variable for score.
我试图把它拿出来,但它一直说它找不到分数的变量。 I'm relatively new to coding, so please tell me a way to save the score, or to stop it from resetting.
我对编码比较陌生,所以请告诉我一种保存分数或阻止它重置的方法。 Here is some of the code in Python (The problem is within #Roll)
这是Python中的一些代码(问题在#Roll内)
#Roll# #卷#
import random
def roll(player):
score = 0
score2 = 0
if player == 1:
print("Rolling for",P1,"...")
dice1 = (random.randrange(1,7))
dice2 = (random.randrange(1,7))
dietotal = dice1 + dice2
score = score + dietotal
dice(dice1)
dice(dice2)
print("Your score so far is", score,)
else:
print("Rolling for",P2,"...")
dice1 = (random.randrange(1,7))
dice2 = (random.randrange(1,7))
dietotal = dice1 + dice2
score2 = score2 + dietotal
dice(dice1)
dice(dice2)
print("Your score so far is", score2,)
#Accounts# #账户#
password = 'wrong'
print("Welcome to the dice game!")
P1 = input("What is player 1's name?")
while password != 'right':
password = input("What is the password?")
if password == 'password':
print("Ok", P1,"welcome!")
password = 'right'
else:
print("Password is invalid, try again")
P2 = input("What is player 2's name?")
while password != 'wrong':
password = input("What is the password?")
if password == 'password':
print("Ok", P2,"welcome!")
password = 'wrong'
else:
print("Password is invalid, try again")
#Game# #游戏#
rounds = 0
while rounds != 5:
print("Its your turn to roll", P1,)
start = input("Enter roll when you want to")
while start != 'roll':
print("This is invalid")
start = input("Enter roll when you want to")
else:
roll(1)
print("Its your turn to roll", P2,)
start = input("Enter roll when you want to")
while start != 'roll':
print("This is invalid")
start = input("Enter roll when you want to")
else:
roll(2)
rounds = rounds + 1
Try making your roll function return the result.尝试让您的卷 function 返回结果。 In #Roll# put return score and return score2 where you have your print statements then remove the print statements.
在#Roll# 中,将返回分数和返回分数2 放在您有打印语句的位置,然后删除打印语句。
In #Game# make the following changes:在#Game# 中进行以下更改:
Define vars score and score2定义 vars score 和 score2
Change roll(1) -> score=score + roll(1) print("Your score so far is", score,)更改 roll(1) -> score=score + roll(1) print("你目前的分数是", score,)
and和
Change roll(2) -> score2=score2 + roll(2) print("Your score so far is", score2,)更改 roll(2) -> score2=score2 + roll(2) print("你目前的分数是", score2,)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.