英文:
Variables being seen as strings
问题
以下是翻译好的部分:
我正在尝试将我用JavaScript编写的程序转换为Python,以便在学校的计算器上使用,但我一直遇到一些毫无道理的问题。经常在运行程序时,它会将变量视为字符串,为了解决这个问题,我尝试使用int()
函数。它在计算部分之前有效。但当变量等于零时,我不断看到错误,比如“int()函数的无效文字”。
跟踪信息:
Traceback (most recent call last):
File "main.py", line 16, in <module>
meltice = (mass*(3.34*(10**5)))
TypeError: can't multiply sequence by non-int of type 'float'
我尝试将每个变量都放入int()
函数中,但出现了上述错误。我认为这可能与顶部的input()
函数有关。
英文:
I am trying to convert a program I made in JavaScript into Python so I can use it on a school calculator, but I keep running into issues that make no sense. Often when I run the program it sees the variables as strings, and to fix this I tried to use the int()
function. It worked up until the area with the calculations. I keep seeing errors when a variable equals zero, such as "invalid literal for int() with base 10" which I also don't know how to fix.
intltemp = input("initialtemp")
finaltemp = input("finaltemp")
mass = input("mass")
if int(intltemp) < 0:
warmicetemp = intltemp
else:
warmicetemp = 0
if int(finaltemp) > 100:
steamtemp=finaltemp
heatwatertemp = 100
else:
steamtemp = 0
heatwatertemp = finaltemp
warmice = (mass*warmicetemp*2060)
meltice = (mass*(3.34*(10**5)))
heatwater = (mass*heatwatertemp*4184)
vaporwater = (mass*(2.26*(10**6)))
steam = (mass*steamtemp*2020)
final = warmice+meltice+heatwater+vaporwater+steam
print(final)
The traceback:
Traceback (most recent call last):
File "main.py", line 16, in <module>
meltice = (mass*(3.34*(10**5)))
TypeError: can't multiply sequence by non-int of type 'float'
I tried making every variable inside an int()
function, and it came up with the aforementioned error. I believe it could be related to the input()
function at the top.
答案1
得分: 1
以下是您要翻译的内容:
在这一行中,您正在执行 str * str * int
warmice = (mass * warmicetemp * 2060)
对于所有其他变量也可以说是一样的。
您需要使用 int()
进行转换,否则您将在整数和字符串之间进行乘法运算。
尝试这个:
intltemp = int(input("initialtemp"))
finaltemp = int(input("finaltemp"))
mass = int(input("mass"))
if intltemp < 0:
warmicetemp = intltemp
else:
warmicetemp = 0
if finaltemp > 100:
steamtemp = finaltemp
heatwatertemp = 100
else:
steamtemp = 0
heatwatertemp = finaltemp
warmice = (mass * warmicetemp * 2060)
meltice = (mass * (3.34 * (105)))
heatwater = (mass * heatwatertemp * 4184)
vaporwater = (mass * (2.26 * (106)))
steam = (mass * steamtemp * 2020)
final = warmice + meltice + heatwater + vaporwater + steam
print(final)
英文:
In this line you are doing str * str * int
warmice = (mass*warmicetemp*2060)
The same can be said for all your other variables.
You need to convert them to int using int()
otherwise you are multiplying ints by strings.
Try this:
intltemp = int(input("initialtemp"))
finaltemp = int(input("finaltemp"))
mass = int(input("mass"))
if intltemp < 0:
warmicetemp = intltemp
else:
warmicetemp = 0
if finaltemp > 100:
steamtemp=finaltemp
heatwatertemp = 100
else:
steamtemp = 0
heatwatertemp = finaltemp
warmice = (mass*warmicetemp*2060)
meltice = (mass*(3.34*(10**5)))
heatwater = (mass*heatwatertemp*4184)
vaporwater = (mass*(2.26*(10**6)))
steam = (mass*steamtemp*2020)
final = warmice+meltice+heatwater+vaporwater+steam
print(final)
答案2
得分: 0
问题是输入函数将输入读取为字符串而不是整数。
我确保在开头的input()函数是整数而不是包含整数的字符串。我通过使用int()函数将输入转换为整数来实现这一点。
英文:
The issue was that the input function was reading the input as a string and not an integer.
I made sure the input() function at the beginning was an integer and not a string containing an integer. I acheived this by using the int() function to turn the input as a string into integer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论