簡體   English   中英

為什么系統只通過掃描第一個數字來決定哪個更大 - Python

[英]why does the system decides which is bigger by scanning only the first number - Python

這是我的代碼,當我輸入有兩位數字和一個只有一位數字的數字時(例如,當我輸入 47、57 和 9 時),結果為 9,這是最小的。 請幫忙。

def maximum(a, b, c):
        list = [a, b, c]
        return max(list)


num_one = input("Enter 1st Number: ")
num_two = input("Enter 2nd Number: ")
num_three = input("Enter 3rd Number: ")
num4 = 0

if num_one.isdecimal():
    if num_one.isdecimal():
        if num_one.isdecimal():
            a = num_one
            b = num_two
            c = num_three
            print(maximum(a, b, c))
        else:
            print("wrong input")
    else:
        print("wrong input")
else:
    print("wrong input")

當您使用input()時,它會自動接收一個字符串。 因此,您必須稍后將其轉換為浮點數或 integer。 此外,關鍵字and代替使用三個if語句很有用:

def maximum(a, b, c):
    list = [a, b, c]
    return max(list)

num_one = input("Enter 1st Number: ")
num_two = input("Enter 2nd Number: ")
num_three = input("Enter 3rd Number: ")

if num_one.isdecimal() and num_two.isdecimal() and num_three.isdecimal():
    a = float(num_one)
    b = float(num_two)
    c = float(num_three)
    print(maximum(a, b, c))
else:
    print('Wrong input')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM