【问题标题】:ValueError: invalid literal for int() with base 10: '', pls help to fix itValueError: int() 以 10 为基数的无效文字:'',请帮助修复它
【发布时间】:2021-10-24 18:31:53
【问题描述】:

我正在学习 python 并且正在做一个练习集。当我遇到一个错误说这是整个代码时,顺便说一句,代码是打印最大的数字,并且在输入输入并将其类型转换为 int 时发生错误。

     def greatest(num1, num2, num3):
if(num1 >= num2 and num1 >= num2):
    print("num1 is greatest")
elif(num2 >= num1 and num2 >= num3):
    print("num2 is greatest")
elif(num3 >= num1 and num3 >= num2):
    print("num3 is greatest")
int1 = int(input("enter a number1"))
int2 = int(input("enter a number2"))
int3 = int(input("enter a number3"))
greatest(int1, int2, int3)  

错误是

  enter a number1 
  Traceback (most recent call last):
  File "c:\Users\DELL\Desktop\Python\8. Chapter 8\practice.py", line 8, in
  <module>
  int1 = int(input("enter a number1"))
  ValueError: invalid literal for int() with base 10: ''
  PS C:\Users\DELL\Desktop\Python>

【问题讨论】:

  • 如果您不输入任何内容,input 将返回 ''int 无法将 '' 转换为整数

标签: python function input valueerror


【解决方案1】:

Aryan Agarwal 代码没有问题。 我认为您必须在没有输入任何值的情况下按 Enter。

这就是为什么 input return '' 和 int() 不能将 '' 转换为整数的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-06
    • 2017-06-18
    • 2013-08-04
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多