2016-11-19 56 views
3

我想用python製作一個非常簡單的遊戲。在Python中的While循環上需要幫助

遊戲是蛇梯,玩家從開始進入到結束(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!") 

但問題是,循環根本不會開始。它說:

「在分配之前引用的局部變量'player_score'」。

另請參見:如何通過已添加的player_score再次繼續遊戲至player_choice?

回答

0

定義變量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!") 

內,比賽結束後,你可以再次從用戶請求再次運行遊戲。如果他接受,然後再從你開始你的遊戲調用你的方法

+0

嗨,非常感謝!我可以再問一件事。現在遊戲運行,骰子不會每次滾動一個隨機數。它總是停留在相同的數字。它出什麼問題了 ? –

+0

也可以在擲骰子的函數中定義數字。 如果上面的答案是正確的,請接受答案,這是正確的答案。 –

0

您可以嘗試

def print_board(board): 
    print (" ".join(board[0:11])) #first row 
    print (" ".join(board[11:20])) #second row 
    print (" ".join(board[21:30])) #third row 
    print (" ".join(board[31:40])) #fourth row 
    print (" ".join(board[41:50])) #fifth row 
    print (" ".join(board[51:60])) #six row 
    print (" ".join(board[61:70])) #seventh row 
    print (" ".join(board[71:80])) #eight row 
    print (" ".join(board[81:90])) #ninth row 
    print (" ".join(board[91:100])) 

def roll_dice(): #function to roll the dice 
    dice = randint(1, 6) 
    print ("Rolling the dice...") 
    sleep(1.5) 
    print ("..."), dice 
    return dice 
#game starts 
def main(): 
    player_score = 0 

    print ("Let's play a game") 
    while player_score < 100: 
     print_board(board) 
     player_choice = raw_input("put R for Rolling or I for Information: ") 
     player_choice = player_choice.upper() 
     if player_choice == "R": 
      dice = roll_dice() 
      if dice == 1: 
       player_score = player_score + 1 
       print "Player moves.. ", dice 
      elif dice == 2: 
       player_score = player_score + 2 
       print "Player moves.. ", dice 
      elif dice == 3: 
       player_score = player_score + 3 
       print "Player moves.. ", dice 
      elif dice == 4: 
       player_score = player_score + 4 
       print "Player moves.. ", dice 
      elif dice == 5: 
       player_score = player_score + 5 
       print "Player moves..",dice 

      elif dice == 6: 
       player_score = player_score + 6 
       print "Player moves..", dice 

      print "You are now at %d" % player_score 
    elif player_choice == "I": 
     print "You are now at %d" % player_score 
else: 
    print ("Congratulations, you won the game!") 

你看到的原因raw_input()是因爲input()給出了R不是定義的錯誤和原因,你看到有輕微索引編制的改變是因爲你的代碼產生了一些數字兩次 而你看到沒有播放器的功能,因爲只有一個命令