简体   繁体   English

Python ValueError:以 10 为基数的 int() 文字无效:

[英]Python ValueError: invalid literal for int() with base 10:

I am trying to write a program to do some simple power usage and cost calculations, the logic is correct and the algorithm works for VB but I want to use Python.我正在尝试编写一个程序来进行一些简单的功耗和成本计算,逻辑是正确的,算法适用于 VB,但我想使用 Python。 Being new to Python I am a little confused as to what I am missing, the code is below.作为 Python 的新手,我对缺少的东西感到有些困惑,代码如下。 I have used some conventions here to help me understand what is happening我在这里使用了一些约定来帮助我了解正在发生的事情

IntWatts = input ("What is the Watt rating? ")
IntHoursOfUse = input ("How many hours is the device on for? ")
IntTariff = input ("What is the Tariff in cents? ") 

IntEnergyRating = int(IntWatts)/1000
IntUsageKWH = int(IntEnergyRating) * IntHoursOfUse
IntCostInCents = IntUsageKWH * int(IntTariff)
IntCostInDollars = float(IntCostInCents) / 100 

print('the Cent cost is :',IntCostInCents)
print("the Dollar cost is ;", IntCostInDollars)enter code here

I have been using the inputs of Watts 240 HoursOfUse 5 Tariff 34.1我一直在使用 Watts 240 HoursOfUse 5 Tariff 34.1 的输入

Thanks谢谢

As to why the error is happening, it is simply a matter of typecasting the wrong variable, note the example below.至于为什么会发生错误,这只是类型转换错误变量的问题,请注意下面的示例。

watts = input('What is the Watt rating?')
hours_of_use = input('How many hours is the device on for?')
tariff = input('What is the Tariff in cents?')

energy_rating = int(watts) / 1000
usage_kwh = energy_rating * int(hours_of_use)                                     
cost_in_cents = usage_kwh * int(tariff)
cost_in_dollars = cost_in_cents / 100

print('The cent cost is :', cost_in_cents)
print('The dollar cost is :', cost_in_dollars)

This code should produce the results you are looking for.此代码应生成您正在寻找的结果。 As to what some of the problems are here.至于这里有什么问题。

A few things to note, you only need to cast the input() values here since they are coming in as strings and need to be interpreted in your program as integers.需要注意的几件事,您只需要在此处转换 input() 值,因为它们是作为字符串传入的,并且需要在您的程序中被解释为整数。

In Python there are two forms of division / which results in expected results according to how we as humans learn math, and // which will floor your result;在 Python 中,有两种形式的除法/根据我们人类学习数学的方式产生预期的结果,以及//这将决定你的结果; this is why you do not need to cast to floats.这就是为什么您不需要转换为浮点数的原因。

There are many pythonic things here to take away as you learn this language, while I won't go into great depth, do note the naming conventions.当你学习这门语言时,这里有很多 python 的东西要带走,虽然我不会深入研究,但请注意命名约定。 Python variables are typically _ delimited, lowercase and self-documenting. Python 变量通常是_分隔的、小写的和自我记录的。 Additionally it is considered fairly poor practice to label variables with type, this is an old convention that has fallen out of practice.此外,使用类型标记变量被认为是相当糟糕的做法,这是一个已经过时的旧约定。

For additional reading on Python, check out the coding guidelines: https://web.archive.org/web/20111010053227/http://jaynes.colorado.edu/PythonGuidelines.html#module_formatting有关 Python 的更多阅读,请查看编码指南: https : //web.archive.org/web/20111010053227/http : //jaynes.colorado.edu/PythonGuidelines.html#module_formatting

you need to type-cast IntHoursOfUse to int while initializing IntUsageKWH as:您需要在初始化IntUsageKWH时将IntUsageKWH类型IntHoursOfUseint

#                                  v  Type casting this to int()
IntUsageKWH = IntEnergyRating * int(IntHoursOfUse)
#             ^ No need to type-cast this as you are already doing it in previous line

I needed to change the inputs to be我需要将输入更改为

IntWatts = int(input ("What is the Watt rating? "))
IntHoursOfUse = int(input ("How many hours is the device on for? "))
IntTariff = float(input ("What is the Tariff in cents? "))

and then remove all other int/float commands然后删除所有其他 int/float 命令

Thanks for the help谢谢您的帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM