简体   繁体   English

我在运行程序“ ValueError:int()的基数为10的无效文字:”时遇到此错误:

[英]im experiencing this error when running a program “ValueError: invalid literal for int() with base 10:”

i am attempting to build a composite function calculator, but I'm running into some issues. 我正在尝试构建复合函数计算器,但遇到了一些问题。

import math

xval = input(int("what is the value of X?"))

fval = input(str("what is the value of f?"))

gval = input(str("what is the value of f?"))

this is the bit of code that isn't working. 这是一些无效的代码。 For the "xval" variable i keep on getting the error message 对于“ xval”变量,我不断收到错误消息

ValueError: invalid literal for int() with base 10: 'what is the value of X?' ValueError:以10为底的int()的无效文字:“ X的值是多少?”

I've looked around online, but I can't find any issues with my code. 我在线上四处看看,但是我的代码找不到任何问题。

You have to first execute input and then int to cast the result into an integer number. 您必须先执行input ,然后执行int才能将结果转换为整数。

Otherwise you are trying to convert a text into an integer 否则,您将尝试将文本转换为整数

xval = int(input("what is the value of X?"))

In the other lines, the str is redundant, but is it really what you want? 在其他方面, str是多余的,但是它真的是您想要的吗? I mean to me it looks like fval and gval should also be numbers. 我的意思是fvalgval应该也是数字。

Ah, and the text for gval should be "what is the value of g " 嗯,gval的文本应该是“ g的值是什么”

Also take in account that int will convert your inputs to integers, if you need decimal numbers consider using float() 还要考虑到int会将输入转换为整数,如果需要十进制数字,请考虑使用float()

Explanation: 说明:

Your original line input(int("what is the value of X?")) basically does the following steps: 您原始的行input(int("what is the value of X?"))基本上执行以下步骤:

  1. Creates the string "what is the value of X?" 创建字符串“ X的值是什么?”
  2. Tries to convert this string to an integer number (and fails) 尝试将此字符串转换为整数(失败)
  3. Supposing point 2 doesn't fail (which does), then it will execute input giving an integer as text (and failing again) and storing your input as a string. 假设点2不会失败(会失败),那么它将执行input并给出一个整数作为文本(并再次失败)并将输入存储为字符串。 As a result your variable xval would be a string (which is not what you want) 结果,您的变量xval将是一个字符串(这不是您想要的)

Instead, what you want is to first execute input and convert the value that you type into an integer, so you have to execute: 相反,您想要的是首先执行input并将input的值转换为整数,因此必须执行:

xval = int(input("what is the value of X?"))

Note that if you input a string, then the cast into int() will fail, you might want to use try and catch a possible exeption. 请注意,如果输入字符串,则强制转换为int()会失败,您可能需要使用try并捕获可能的示例。

If you are using Python3.x, Python 3.x doesn't evaluate and convert the data type, you have to explicitly convert to int 如果您使用的是Python3.x,则Python 3.x不会评估和转换数据类型,您必须显式转换为int

x = int(input("what is the value of X?"))

If you are using Python2.x, you need to do like this: 如果您使用的是Python2.x,则需要执行以下操作:

x = input("what is the value of X?")

You can then check the type of x using type(x), you will get <type 'int'> 然后可以使用type(x)检查x的类型,您将得到<type 'int'>

暂无
暂无

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

相关问题 ValueError: int() 的无效文字,以 10 为基数:运行 unittest 时为“30.0” - ValueError: invalid literal for int() with base 10: '30.0' when running unittest 错误 - ValueError:基数为10的int()的文字无效:&#39;&#39; - Error - ValueError: invalid literal for int() with base 10: ' ' 错误:ValueError:int()以10为底的无效文字: - Error: ValueError: invalid literal for int() with base 10: '' ValueError:int() 的无效文字,基数为 10:&#39;&#39; 错误 - ValueError: invalid literal for int() with base 10: '' error 不知道为什么我得到 ValueError: invalid literal for int() with base 10: - Not sure why Im getting ValueError: invalid literal for int() with base 10: 获取错误“ ValueError:int()的无效文字,基数为10:&#39;3128;&#39; 在运行Tensorflow训练示例时 - Getting an error "ValueError: invalid literal for int() with base 10: '3128;' while running the Tensorflow training example Python错误ValueError:以10为底的int()的无效文字 - Python Error ValueError: invalid literal for int() with base 10 ' ' Python 数据错误:ValueError:int() 的无效文字,基数为 10:&#39;42152129.0&#39; - Python data error: ValueError: invalid literal for int() with base 10: '42152129.0' ValueError:以10为底的int()的无效文字:&#39;&#39;无法发现错误 - ValueError: invalid literal for int() with base 10: '' Unable to spot error 这是什么错误? 基数为 10 的 int() 的 ValueError 无效文字:&#39;&#39; - What is this error ? ValueError invalid literal for int() with base 10: ''
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM