如何解决ValueError:以10为基数的int的无效文字出现了IDK解决方案
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:以10为底的int()的无效文字:
帮助...
解决方法
当您输入字母而不是数字时,会发生错误。
此外,在尝试删除该索引上的数字之前,您应该测试用户输入的有效性:
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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。