Python 3.x 尝试检测无效输入时出现未使用的变量错误
我对编程比较陌生,所以我的朋友建议我试着制作一个可定制的骰子滚轮,让我的脚湿透。我有点撞到了墙,他无法发表评论。这是我的密码:Python 3.x 尝试检测无效输入时出现未使用的变量错误,python-3.x,Python 3.x,我对编程比较陌生,所以我的朋友建议我试着制作一个可定制的骰子滚轮,让我的脚湿透。我有点撞到了墙,他无法发表评论。这是我的密码: #Die rolling simulator import random as rnd #Defines total as a set, y as the error detection, and allowed as a set of positive integer values. total = set() y = False allowed = set("12
#Die rolling simulator
import random as rnd
#Defines total as a set, y as the error detection, and allowed as a set of positive integer values.
total = set()
y = False
allowed = set("1234567890")
def dice_roll():
y = False
dice = input("What number of sides should your dice have? ")
#tests if the input for "dice" is a positive whole number, then converts it to an integer.
if dice and allowed.issuperset(dice):
dice = int(dice)
dice_rolls = input("How many times would you like to roll? ")
if dice_rolls and allowed.issuperset(dice_rolls):
dice_rolls = int(dice_rolls)
while dice_rolls >= 1:
dice_rolls -= 1
roll = rnd.randint(1, dice)
total.add(roll)
print(total)
else:
print("Invalid input. Please use only numbers, no decimal points.")
y = True
else:
print("Invalid input. Please use only numbers, no decimal points.")
y = True
def lets_go():
dice_roll()
#tests if the error detection went off, repeats if it did.
if y:
print("Please try again.")
lets_go()
lets_go()
我在这里试图做的是询问一个骰子应该有多少面,以及用户想要多少骰子。如果他们输入的不是一个完整的正数,我希望它说“无效输入。请只使用数字,不要使用小数点。请重试。”然后让代码重复。如果一切顺利,它应该在打印结果后结束
每当我运行这个程序时,除了检查失败的输入之外,一切都正常。例如,如果我输入字母“f”,代码将正确地打印“invalid input”语句,但在没有错误消息的情况下终止整个过程。我还有一个警告,声称y是一个未使用的变量,尽管我在代码开头和dice_roll函数内部定义了它。这是怎么回事