Python的ValueError:int()与基数10的无效文字: [英] Python ValueError: invalid literal for int() with base 10:

查看:585
本文介绍了Python的ValueError:int()与基数10的无效文字:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个程序来做一些简单的电力使用和成本计算,逻辑是正确的,算法适用于VB,但我想使用Python。对Python新手我有点困惑,我缺少什么,代码如下。我在这里使用了一些约定来帮助我理解所发生的事情。

  IntWatts = input(什么是瓦特评级?) 
IntHoursOfUse = input(设备需要多少小时?)
IntTariff = input(什么是费率,以分为单位)

IntEnergyRating = int IntWatts)/ 1000
IntCostInDollars = float(IntCostInCents)/ 100

print((IntEnergyRating)* IntHoursOfUse
IntCostInCents = IntUsageKWH * int(IntTariff) '美元成本是:',IntCostInCents)
print(美元成本是,IntCostInDollars)在这里输入代码

我一直在使用
的输入值240
HoursOfUse 5
费率34.1



谢谢

解决方案

至于错误发生的原因,只是简单的说明了错误的变量,注意下面的例子。 p>

 瓦特=输入('什么是瓦特评级?')
h ($'$'

energy_rating = int(瓦特)/ 1000)
usage_kwh = energy_rating * int(hours_of_use)
cost_in_cents = usage_kwh * int(关税)
cost_in_dollars = cost_in_cents / 100

print('分成本为:' ,cost_in_cents)
print('美元成本是:',cost_in_dollars)

应该产生你正在寻找的结果。至于什么是这里的一些问题。

有几件事要注意,你只需要在这里输入input()值,因为它们是以字符串形式进入的,需要在你的程序中被解释为整数。

在Python中,有两种形式的分割 / 根据我们作为人类学习数学的预期结果,以及 // 这就是为什么你不需要投到浮动。



当你学习这门语言的时候,有很多pythonic东西可以拿走,而我不会进入伟大深入,请注意命名约定。 Python变量通常是 _ 分隔,小写和自我记录。另外,用变量来标注变量被认为是相当差的做法,这是一个已经落后于实践的旧惯例。

有关Python的其他阅读,请查阅编码准则:
https://web.archive.org/web/20111010053227/http://jaynes.colorado.edu/PythonGuidelines.html# module_formatting


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. Being new to Python I am a little confused as to what I am missing, the code is below. 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

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.

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; 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 variables are typically _ delimited, lowercase and self-documenting. 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的ValueError:int()与基数10的无效文字:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆