python-2.7 - 在 Python 中需要有关 While 循环的帮助

标签 python-2.7 python-3.x

<分区>

我正在尝试使用 python 制作一个非常简单的游戏。

游戏是蛇梯,玩家从头到尾 (0 - 100)

from random import randint
from time import sleep

board = ["1" ,"2", "3", "4", "5", "6", "7", "8", "9", "10", "11" ,"12", "13", "14", "15",
     "16", "17", "18", "19", "20", "21" ,"22", "23", "24", "25", "26", "27", "28", "29",
     "30", "31" ,"32", "33", "34", "35", "36", "37", "38", "39", "40", "41" ,"42", "43",
     "44", "45", "46", "47", "48", "49", "50", "51" ,"52", "53", "54", "55", "56", "57",
     "58", "59", "60", "61" ,"62", "63", "64", "65", "66", "67", "68", "69", "70", "71",
     "72", "73", "74", "75", "76", "77", "78", "79", "80", "81" ,"82", "83", "84", "85",
     "86", "87", "88", "89", "90", "91" ,"92", "93", "94", "95", "96", "97", "98", "99",
     "100"]

def print_board(board):
    print ("   ".join(board[0:10]))    #first row
    print ("   ".join(board[10:20]))    #second row
    print ("   ".join(board[20:30]))    #third row
    print ("   ".join(board[30:40]))    #fourth row
    print ("   ".join(board[40:50]))    #fifth row
    print ("   ".join(board[50:60]))    #six row
    print ("   ".join(board[60:70]))    #seventh row
    print ("   ".join(board[70:80]))    #eight row
    print ("   ".join(board[80:90]))    #ninth row
    print ("   ".join(board[90:100]))    #tenth row


dice = randint(1, 6)   #random number 1 - 6
def roll_dice(dice):       #function to roll the dice
    print ("Rolling the dice...")
    sleep(1.5)
    print ("..."), dice
    return


player_score = 0    #starting point / score

def player(player_score):    #for Information
    print ("You are now at %d") % player_score

def main():                          #game starts
    print ("Let's play a game")
    while player_score < 100:        #This is the problem, the while doesn't recognize player_score
        print_board(board)
        player_choice = input("put R for Rolling or I for Information: ")
        player_choice = player_choice.upper() 
        if player_choice == "R":
            roll_dice(dice)
            if dice == 1:
                player_score = player_score + 1
                print ("Player moves.. ")
                player(player_score)     
            elif dice == 2:
                player_score = player_score + 2
                print ("Player moves.. ")
                player(player_score)
            elif dice == 3:
                player_score = player_score + 3
                print ("Player moves.. ")
                player(player_score)
            elif dice == 4:
                player_score = player_score + 4
                print ("Player moves.. ")
                player(player_score)
            elif dice == 5:
                player_score = player_score + 5
                print ("Player moves.. ")
                player(player_score)
            elif dice == 6:
                player_score = player_score + 6
                print ("Player moves.. ")
                player(player_score)
        elif player_choice == "I":
            player(player_score)
    else:
        print ("Congratulations, you won the game!")

但问题是,循环甚至没有开始。它说:

"local variable 'player_score' referenced before assignment".

另外:我怎样才能在已经添加了 player_score 的情况下再次继续游戏到 player_choice?

最佳答案

在 main 中定义变量 player_score = 0

def main(): 
    player_score = 0                         #game starts
    print ("Let's play a game")
    while player_score < 100:        #This is the problem, the while doesn't recognize player_score
        print_board(board)
        player_choice = input("put R for Rolling or I for Information: ")
        player_choice = player_choice.upper() 
        if player_choice == "R":
            roll_dice(dice)
            if dice == 1:
                player_score = player_score + 1
                print ("Player moves.. ")
                player(player_score)     
            elif dice == 2:
                player_score = player_score + 2
                print ("Player moves.. ")
                player(player_score)
            elif dice == 3:
                player_score = player_score + 3
                print ("Player moves.. ")
                player(player_score)
            elif dice == 4:
                player_score = player_score + 4
                print ("Player moves.. ")
                player(player_score)
            elif dice == 5:
                player_score = player_score + 5
                print ("Player moves.. ")
                player(player_score)
            elif dice == 6:
                player_score = player_score + 6
                print ("Player moves.. ")
                player(player_score)
        elif player_choice == "I":
            player(player_score)
    else:
        print ("Congratulations, you won the game!")

游戏结束后,您可以再次请求用户再次运行游戏。如果他接受然后再次调用你的方法从你开始你的游戏

关于python-2.7 - 在 Python 中需要有关 While 循环的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40689109/

相关文章:

python - 如何设置 raw_input 的时间限制

python-3.x - 让 PyC​​harm 将字符串识别为路径

python - Python3 中德语元音变音的编码/解码

python-3.x - 如何在 VS Code 中正确导入 Python 模块?

python - 如何将类成员方法的参数列表自动填充到可变参数中?

python - 如何找到字符串的可能组合总数?

python - 类没有属性,即使已设置

python - 我可以在 Python 中收到始终评估为 true 的警告吗?

python-2.7 - pyqtgraph删除pyqt4 gui中的持久图例

python-2.7 - django-dynamic-scraper : No module named django. core.management