【问题标题】:ValueError: invalid literal for int() with base 10: '' has occurred idk solutionValueError: invalid literal for int() with base 10: '' has occurred idk 解决方案
【发布时间】:2020-08-11 13:28:08
【问题描述】:

dice = [1, 2, 3, 4, 5, 6]
first = [random.choice(dice) for i in range(5)]
print(first)

while True:
    baggage = []
    retry = random.choice(dice)
    F = input("Choose the dice you want to roll again. (if you don't want to roll again, enter the '0'.)>")
    baggage.append(F)
    del first[int(F)-1]
    first.insert(int(F)-1, retry)
    if F == '0' or len(baggage) > 5:
        break

print(first)

run > ValueError: int() 以 10 为底的无效文字:''

帮助...

【问题讨论】:

  • 运行程序时输入的输入值是多少?
  • 介于 1 到 5...
  • @Aziz 错误消息显示,它是一个空字符串文字
  • @MZ omg 我意识到我的错误谢谢????????

标签: python python-3.x


【解决方案1】:

当您输入字母而不是数字时会发生错误。

此外,您应该在尝试删除该索引上的数字之前测试用户输入的有效性:

from numpy import random

dice = [1, 2, 3, 4, 5, 6]
first = [random.choice(dice) for i in range(5)]
print(first)

while True:
    baggage = []
    retry = random.choice(dice)
    F = input("Choose the dice you want to roll again. (if you don't want to roll again, enter the '0'.)>")
    if F == '0' or len(baggage) > 5 or F.isalpha() or int(F)>5:
        break
    else:
        baggage.append(F)
        del first[int(F)-1]
        first.insert(int(F)-1, retry)

print(first)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-25
    • 2022-01-09
    • 2019-04-16
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 2021-08-11
    相关资源
    最近更新 更多