Python ValueError:以10为基数的int()的文本无效:'';,如何使用用户输入的值作为列表中的索引
我试图教自己如何编程,从而建立一个玩家yahtzee游戏(之前没有任何经验),并已达到用户输入他希望重新掷骰子的程度Python ValueError:以10为基数的int()的文本无效:'';,如何使用用户输入的值作为列表中的索引,python,Python,我试图教自己如何编程,从而建立一个玩家yahtzee游戏(之前没有任何经验),并已达到用户输入他希望重新掷骰子的程度 while True: turn1 = raw_input("Would you like to cast your first throw?") if (turn1 == "y"): first_roll = roll() print_all_5(first_roll) print("You have rolled a: " + " ".join
while True:
turn1 = raw_input("Would you like to cast your first throw?")
if (turn1 == "y"):
first_roll = roll()
print_all_5(first_roll)
print("You have rolled a: " + " ".join(str(first_roll)))
if (raw_input("Would you like to reroll any if your dice?") == "y"):
print("Please input which dice you would like to reroll (first, second, third etc) with one digit")
rerolled_dice1 = raw_input("I want to reroll dice/dices number: ")
for x in rerolled_dice1:
first_roll[int(x) -1] = randint(1,6)
elif (turn1 == "n"):
print("Well why even bother wasting your battery life to run these measily 106 lines of code?")
else:
print("Respond only with 'y' for yes and 'n' for no please x_X")
我试图做的是将用户输入的值存储到列表变量rerolled_dice1中,然后对其进行迭代,将存储为str的值转换为int,并将其用作first_roll的索引,该变量保存与原始掷骰集对应的值,并将其更改为随机值号码
当我运行代码时,我得到的回溯是:
Traceback (most recent call last):
File "C:\Python27\projects\oneplayer.py", line 110, in <module>
first_roll[int(x) -1] = randint(1,6)
ValueError: invalid literal for int() with base 10: ''
你是说
int(x)
而不是int(重新滚动的骰子1)
?你还会注意到一种模式dice3
的作用与dice1
和dice2
相同dice 5
完成dice 6所做的一切,除了dice[1][2]=“O”
。感谢您的帮助,同时更新了回溯。你的意思是我只需要几个函数就可以写骰子了吗?情节越来越复杂了。在重新滚动的骰子1.strip().split()中为x使用:
,非常感谢;D
from random import randint
def roll():
dice_values = []
for x in range (5):
dice = randint(1,6)
dice_values.append(dice)
return dice_values
def create_dice():
dice = [] # create empty list - main dice
for i in range(3): #loop building a dice list containing three row sublists
row = ["*"] * 3 #create row containing three lists each holding a str
dice.append(row) #append row list to dice list
return dice #!! important return dice list from function
def print_dice(dice): # prints out dice list neatly
for row in dice: # uses the index to access lst value in dice lst
print(" ".join(row)) # joins strings with space in between
def dice1 (dice):
dice[1][1] = "O"
return dice
def dice2 (dice):
dice[0][0] = "O"
dice[2][2] = "O"
return dice
def dice3 (dice):
dice[0][0] = "O"
dice[1][1] = "O"
dice[2][2] = "O"
return dice
def dice4 (dice):
dice[0][0] = "O"
dice[0][2] = "O"
dice[2][0] = "O"
dice[2][2] = "O"
return dice
def dice5 (dice):
dice[0][0] = "O"
dice[0][2] = "O"
dice[1][1] = "O"
dice[2][0] = "O"
dice[2][2] = "O"
return dice
def dice6 (dice):
dice[0][0] = "O"
dice[0][2] = "O"
dice[1][0] = "O"
dice[1][2] = "O"
dice[2][0] = "O"
dice[2][2] = "O"
return dice
def print_all_5(elist):
print("\n")
for x in elist:
if x == 1:
print(print_dice(dice1(create_dice())))
elif x == 2:
print(print_dice(dice2(create_dice())))
elif x == 3:
print(print_dice(dice3(create_dice())))
elif x == 4:
print(print_dice(dice4(create_dice())))
elif x == 5:
print(print_dice(dice5(create_dice())))
else:
print(print_dice(dice6(create_dice())))
print("\n")