Skip to main content
 首页 » 编程设计

Python 3之ValueError int()base 必须 >=2 且 =36

2025年05月04日9myhome

<分区>

我是一个菜鸟所以请原谅我乱七八糟的困惑代码。我正在尝试制作自己的游戏引擎,用于基于文本的战斗冒险等。这是代码:

import random 
 
def drawBoard(board): 
    # This function prints out the board that it was passed. 
    # "board" is a list of 29 strings representing the board (ignore index 0) 
    print('|---+---+---+---+---+---+---|') 
    print('|   |   |   |   |   |   |   |') 
    print('| ' + board[1] + ' | ' + board[2] + ' | ' + board[3] + ' | ' + board[4] + ' | ' + board[5] + ' | ' + board[6] + ' | ' + board[7] + ' | ') 
    print('|   |   |   |   |   |   |   |') 
    print('|---+---+---+---+---+---+---|') 
    print('|   |   |   |   |   |   |   |') 
    print('| ' + board[8] + ' | ' + board[9] + ' | ' + board[10] + ' | ' + board[11] + ' | ' + board[12] + ' | ' + board[13] + ' | ' + board[14] + ' | ') 
    print('|   |   |   |   |   |   |   |') 
    print('|---+---+---+---+---+---+---|') 
    print('|   |   |   |   |   |   |   |') 
    print('| ' + board[15] + ' | ' + board[16] + ' | ' + board[17] + ' | ' + board[18] + ' | ' + board[19] + ' | ' + board[20] + ' | ' + board[21] + ' | ') 
    print('|   |   |   |   |   |   |   |') 
    print('|---+---+---+---+---+---+---|') 
    print('|   |   |   |   |   |   |   |') 
    print('| ' + board[22] + ' | ' + board[23] + ' | ' + board[24] + ' | ' + board[25] + ' | ' + board[26] + ' | ' + board[27] + ' | ' + board[28] + ' | ') 
    print('|   |   |   |   |   |   |   |') 
    print('|---+---+---+---+---+---+---|') 
 
 
def whoGoesFirst(): 
    if random.randint(0, 1) == 0: # coin flip 50/50 who starts combat 
        firstToSpawn = enemy 
        lastToSpawn = player 
    else: 
        firstToSpawn = player 
        lastToSpawn = enemy 
 
# following chunk works out the spawn points of the first to enter 
#and the last to enter, respectively. It then places them on the 
#board in their respective spawn points, represented by their symbol 
 
while firstToSpawn not in theBoard and lastToSpawn not in theBoard: 
    boardSpawn = '1 2 3 4 5 6 7 14 21 28 27 26 25 24 23 22 15 8'.split() 
    boardSpawnResult = random.choice(boardSpawn) 
    theBoard[int(boardSpawnResult)] = firstToSpawn 
    print(firstToSpawn + ' has entered the room.') 
 
else: 
    if firstToSpawn in theBoard and lastToSpawn not in theBoard: 
        boardHalf = len(boardSpawn) / 2 
        boardHalf = int(boardHalf) 
        lastToSpawnPosition = int(boardSpawnResult) + boardHalf 
        if lastToSpawnPosition > len(boardSpawn): 
            lastToSpawnPosition = lastToSpawnPosition - len(boardSpawn) 
            lastToSpawnPosition = boardSpawn[lastToSpawnPosition] 
        theBoard[int(lastToSpawnPosition)] = lastToSpawn 
        print(lastToSpawn + ' has entered the room.') 
        if firstToSpawn == player: 
            playerLocale = int(boardSpawnResult) 
            global playerLocale 
        else: 
            playerLocale = int(lastToSpawnPosition) 
            global playerLocale 
        print(drawBoard(theBoard)) 
 
 
def shoot(): 
shotChance = int(playerStats[1]) - int(gangerStats[3]) 
didItHit = random.randint(int(shotChance), 100) 
if didItHit < 65: 
    print(playerName + ' Missed the shot!') 
elif didItHit >= 65: 
    didItCrit = random.randint(int(playerStats[5], 85)) 
    if didItCrit >= 85: 
        shotDamage = 25 + weaponsGlock 
        print('Critical hit! ' + shotDamage) 
    else: 
        shotDamage = weaponsGlock 
        print(playerName + ' Hit the enemy for ' + shotDamage) 
 
    gangerStats[0] - shotDamage 
    if gangerStats[0] < 1: 
        print(playerName + ' killed the Ganger!') 
 
#declaring globals and starting the program 
while True: 
    theBoard = [' ']* 29 
    player = ' ' 
    enemy = '!' 
    print('What\'s your name, Droog?') 
    playerName = input() 
    player = playerName[0].upper() 
    playerLocale = ' ' 
    playerStats = '100 50 2 10 5 2'.split() 
    gangerStats = '65 20 1 5 3 5'.split() 
    weaponsGlock = 30 
    whoGoesFirst() 
    shoot() 
while int(gangerStats[0]) > 0: 
    command = input('Type shoot to attack again:').lower 
    if command == 'shoot': 
        shoot() 
else: 
    if int(gangerStats[0]) <= 0: 
        command = input('Type "searchbody" to loot dead enemies:').lower 

错误不断出现在我的 shoot() 函数中,在为 didItCrit 变量滚动 randint 的那一行。这是错误的副本:

Traceback (most recent call last): 
  File "<string>", line 420, in run_nodebug 
  File "C:\Python33\practice programs\textAdventure\combatmodule.py", line 163, in <module> 
    shoot() 
  File "C:\Python33\practice programs\textAdventure\combatmodule.py", line 133, in shoot 
    didItCrit = random.randint(int(playerStats[5], 85)) 
ValueError: int() base must be >= 2 and <= 36 

如有任何帮助,我们将不胜感激。