Python遇到str/int乘法的问题
所以,我试着乘一个变量中指定的数字,但是每当我乘的时候,它就把它当作一个字符串,然后乘这个字符串,但是当我试着把这个变量变成int时,语法说它一定是str 我已经尝试在字符串之前将var转换为intPython遇到str/int乘法的问题,python,function,Python,Function,所以,我试着乘一个变量中指定的数字,但是每当我乘的时候,它就把它当作一个字符串,然后乘这个字符串,但是当我试着把这个变量变成int时,语法说它一定是str 我已经尝试在字符串之前将var转换为int def gamble_menu(): global dice_amount dice_amount = input("How many dice? (higher dice count = higher wager multiplier) ") gamble_dice() def ga
def gamble_menu():
global dice_amount
dice_amount = input("How many dice? (higher dice count = higher wager multiplier) ")
gamble_dice()
def gamble_dice():
while True:
print(str(dice_amount)); print("1 - " + (int(dice_amount)) * 6)
print("You must guess the number rolled from the number of dice")
print("Multiplier = *2")
number_guess = input("#? ")
if (int(number_guess)) < 0 or (int(number_guess)) > dice_amount * 6:
print("Invalid integer, try again")
我希望它将变量乘以一个数字,但它采用的是文字形式,看起来你的骰子数量是一个字符串。intnumber\u guess>dice\u amount*6正在尝试将整数与字符串进行比较。请尝试使用intdice\U amount。可能是因为您试图在打印函数中包含一个数字和一个字符串
试试打印1-+strintdice\u amount*6我想问题出在
print("1 - " + (int(dice_amount)) * 6)
您不能混合使用str+int。如果您像我一样使用python 3.7,请尝试:
print(f"1 - {int(dice_amount) * 6}")
def gamble_菜单:
全球骰子数量
骰子数量=总共有多少个骰子?骰子数越高=下注乘数越高
赌博
def赌博骰子:
尽管如此:
打印金额;打印1-+strintdice_数量*6
打印您必须根据骰子的数量猜出掷骰子的数量
printMultiplier=*2
数字猜=输入?
如果intnumber\U GUSE<0或intnumber\U GUSE>掷骰数量*6: 打印无效的整数,请重试 print1-+intdice\u amount*6这是str+int,这是错误的。 打印1-+strintdice_数量*6这个str+str,它可以工作。
尽我所能帮助您。我假设您包含的代码不完整。请添加更多详细信息。我已经修复了几个错误,主要是由于您在Python中使用字符串的方式。 这个函数试图做什么还不清楚
def gamble_dice(dice_amount):
while True:
print(str(dice_amount))
print("1 - ", (dice_amount * 6))
print("You must guess the number rolled from the number of dice")
print("Multiplier = *2")
number_guess = input("#? ")
if number_guess < 0 or (number_guess > dice_amount * 6):
print("Invalid integer, try again")
else:
print("right on!")
dice_amount = int(input("How many dice? (higher dice count = higher wager multiplier) "))
gamble_dice(dice_amount)
如果intnumber\u GUSE<0或intnumber\u GUSE>dice\u amount*6:意思是:如果intnumber\u GUSE<0或intnumber\u GUSE>strdice\u amount*6:您可以使用文章底部的链接进行更新。请避免在注释中添加代码或附加信息。回溯最近的调用:文件main.py,第72行,在菜单文件main.py,第51行,在菜单gamble\u菜单文件main.py,第26行,在gamble\u菜单gamble\u dice文件main.py,第31行,在gamble\u dice printstrdice\u amount;print1-+intdice_amount*6 TypeError:必须是str,而不是Int,因为您不能将字符串和整数一起添加到一起,这是有效的!现在它正在按预期工作,谢谢。