简体   繁体   English

我不知道如何保存我制作的这个骰子游戏的分数。 我应该改变什么?

[英]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.

相关问题 我刚开始 python,我正在尝试制作剪刀石头布游戏,但我不知道如何更改字体及其大小 - I've just started python and I'm trying to make a Rock ,Paper ,Scissors game and I can't work out how to change the font and its size Pygame 游戏尝试不起作用,我不知道为什么 - Pygame game attempt not working and i can't figure out why 我似乎无法弄清楚如何生成多个随机数等于10分 - I can't seem to figure out how to generate multiple random numbers to equal a score of 10 Python-我无法弄清的蛇游戏错误 - Python - A bug with my snake game that I can't figure out 无法导入我制作的包。 “模块未找到错误” - Impossible to import a package I made. "ModuleNotFoundError" 我为我的掷骰子游戏添加变量过于复杂 - I've overcomplicated adding variables for my dice roll game 我不知道如何从掷硬币游戏的循环中获得总数 - I can't figure out how to get total number from loops in flip coin game 我想不通这个 function 有什么问题? - I can't figure out what is wrong with this function? 我需要将邮政编码更改为一系列点和短划线(条形码),但我无法弄清楚如何 - I need to change a zip code into a series of dots and dashes (a barcode), but I can't figure out how 我不知道这个素数检查器有什么问题 - I can't figure out what is wrong with this prime checker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM