繁体   English   中英

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

[英]I can't figure out how to save the score on this dice game I've made. What should I change?

我非常接近完成这个简单的游戏,但我一生都无法弄清楚如何使用 def function 保存分数。 每次滚动时,由于 def 中的 score = 0,它会重置为 0。 我试图把它拿出来,但它一直说它找不到分数的变量。 我对编码比较陌生,所以请告诉我一种保存分数或阻止它重置的方法。 这是Python中的一些代码(问题在#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,)

#账户#

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")   

#游戏#

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

尝试让您的卷 function 返回结果。 在#Roll# 中,将返回分数和返回分数2 放在您有打印语句的位置,然后删除打印语句。

在#Game# 中进行以下更改:

定义 vars score 和 score2

更改 roll(1) -> score=score + roll(1) print("你目前的分数是", score,)

更改 roll(2) -> score2=score2 + roll(2) print("你目前的分数是", score2,)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM