Variables being seen as strings.

huangapple go评论76阅读模式
英文:

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(&quot;initialtemp&quot;)
finaltemp = input(&quot;finaltemp&quot;)
mass = input(&quot;mass&quot;)
if int(intltemp) &lt; 0:
    warmicetemp = intltemp
else:
    warmicetemp = 0
if int(finaltemp) &gt; 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 &quot;main.py&quot;, line 16, in &lt;module&gt;
    meltice = (mass*(3.34*(10**5)))
TypeError: can&#39;t multiply sequence by non-int of type &#39;float&#39;

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 * (10
6)))
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(&quot;initialtemp&quot;))
finaltemp = int(input(&quot;finaltemp&quot;))
mass = int(input(&quot;mass&quot;))
if intltemp &lt; 0:
    warmicetemp = intltemp
else:
    warmicetemp = 0
if finaltemp &gt; 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.

huangapple
  • 本文由 发表于 2023年3月31日 03:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75892160.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定