我如何要求用户输入2个数字然后它将检查哪个数字更高并打印更高的数字

时间:2016-01-20 14:43:09

标签: python python-3.x

def displaySmallest(num1, num2):
    print ("enter num 1")
    number1 = int(input())
    print("enter num 2")
    number2 = int(input())
    if (number1>number2):
        print("the first number is larger")
    elif (number2>number1):
        print("The second number is larger")

我不确定如何使用户输入功能

3 个答案:

答案 0 :(得分:3)

将以下代码放入displaySmallest.py

def displaySmallest(num1, num2):
    print ("enter num 1")
    number1 = int(input())
    print("enter num 2")
    number2 = int(input())
    if (number1>number2):
        print("the first number is larger")
    elif (number2>number1):
        print("The second number is larger")

if __name__ == "__main__":
  displaySmallest()

然后在命令行键入python displaySmallest.py

答案 1 :(得分:1)

尝试这样做

def displaySmallest():
print ("enter num 1")
number1 = int(input())
print("enter num 2")
number2 = int(input())
if (number1>number2):
    print("the first number is larger")
elif (number2>number1):
    print("The second number is larger")

displaySmallest()

你不需要在这一行中使用这两个参数: -

  

def displaySmallest( num1 num2 ):

因为您正在从函数中获取输入而没有将值传递给它。虽然,如果你想保留参数,那么你可以尝试这样做:

def displaySmallest(num1, num2):
    number1 = num1
    number2 = num2
    if (number1>number2):
        print("the first number is larger")
    elif (number2>number1):
        print("The second number is larger")

print ("enter num 1")
number1 = int(input())

print("enter num 2")
number2 = int(input())

displaySmallest(number1, number2)

希望这会有所帮助并获得有趣的编码:)。

答案 2 :(得分:1)

以下程序应该按照您的要求执行。请注意,某人可能连续两次输入相同的数字。添加了代码以考虑此类事件。

def main():
    first_number = int(input('Enter the first number: '))
    second_number = int(input('Enter the second number: '))
    if first_number > second_number:
        print(first_number, 'is larger than', second_number)
    elif second_number > first_number:
        print(second_number, 'is larger than', first_number)
    else:
        print(first_number, 'is the same as', second_number)

if __name__ == '__main__':
    main()