簡體   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