【问题标题】:Find greater number of two numbers using function使用函数查找更大的两个数字
【发布时间】:2021-07-24 09:15:24
【问题描述】:

我尝试使用函数找到用户输入的两个数字中较大的数字。请帮我找出我在代码中犯的导致错误结果的错误:

def func1(n1,n2):
    if (n1 > n2):
        print(n1," is greater than ",n2)
    else:
        print(n2," is greater than ",n1)

print("Find which number is greater")
num1 = input("Enter the first number: ")
num2 = input("Enter the second number: ")
func1(num1, num2)    

显示错误的结果:

Find which number is greater
Enter the first number: 10
Enter the second number: 5
5  is greater than  10

【问题讨论】:

  • input() 总是返回一个字符串,您需要在比较之前将这些字符串转换为数字(int()
  • 谢谢一百万,是的,我改成赞num1 = int(input("Enter the first number: "))解决了。
  • 输入函数产生一个字符串值,所以转换输入函数
  • 就个人而言,我不喜欢 int(input...),我使用 Pydantic。看我的例子

标签: python python-3.x


【解决方案1】:

使用pydantic。你可以python -m pip install --user pydantic

Pydantic 将帮助您在数据无效时执行数据验证、对话和友好错误。

from pydantic import BaseModel

class InputValues(BaseModel):
    n1: int
    n2: int


def f(n1,n2):
   # let PyDantic deal with conversation 
    num = InputValues(n1=n1, n2=n2)
    if (num.n1 > num.n2):
        print(f"{n1} is greater than {n2}")
    else:
        print(f"{n2} is greater than {n1}")

# Example:

n = "10"
m = "5"

f(n,m)

为什么使用 Pydantic 而不是简单的转换

int(input...) 如果传递了无效的输入,将会失败。两者的不同之处在于,它会准确地告诉你问题所在。

您可以在pydantic上找到更多信息

【讨论】:

    【解决方案2】:

    通过 f-string -

    def func1(n1,n2):
        print(f'{n1} is greater than {n2}' if (n1> n2) else f'{n1} is greater than {n2}')
    
    
    print("Find which number is greater")
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    func1(num1,num2)   
    

    【讨论】:

    • 你能解释一下什么是 f-string 吗?
    • 阅读here 或观看this 视频。
    • f-strings 不仅比其他格式化方式更易读、更简洁、更不容易出错,而且速度也更快!另外,您可以添加条件来操作字符串:)
    【解决方案3】:

    强制输入 int

    def func1(n1,n2):
        if (n1 > n2):
            print(n1," is greater than ",n2)
        else:
            print(n2," is greater than ",n1)
    
    print("Find which number is greater")
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    func1(num1, num2) 
    

    对于您的功能,您可以在一行中实现这一目标

    def func1(n1,n2):
            return str(n1)+" is greater than "+str(n2) if n1>n2 else str(n2)+" is greater than "+str(n1)
            
    

    【讨论】:

      【解决方案4】:

      原因是,输入函数产生一个字符串值。于是把代码改成like:

      def func1(n1,n2):
          if (n1 > n2):
              print(n1," is greater than ",n2)
          else:
              print(n2," is greater than ",n1)
      
      print("Find which number is greater")
      num1 = int(input("Enter the first number: "))
      num2 = int(input("Enter the second number: "))
      func1(num1,num2)   
      

      输出:

      Find which number is greater
      Enter the first number: 5
      Enter the second number: 10
      10  is greater than  5
      

      【讨论】:

        猜你喜欢
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-03-20
        • 1970-01-01
        • 2017-11-22
        • 1970-01-01
        • 1970-01-01
        • 2017-04-12
        相关资源
        最近更新 更多