python百分比数比较大小

2019-07-02 09:36:00 浏览数 (1)

python是无法识别百分比的,估计你的百分比是string,所以需要转成int

代码语言:javascript复制
# !/usr/bin/python3.4
# -*- coding: utf-8 -*-

# 百分数转为int
def percent_to_int(string):
    if "%" in string:
        newint = int(string.strip("%")) / 100
        return newint
    else:
        print("你输入的不是百分比!")


if __name__ == "__main__":

    a = "50%"
    b = "80%"

    # 比较大小
    if percent_to_int(a) > percent_to_int(b):
        print("a > b")
    elif percent_to_int(a) < percent_to_int(b):
        print("a < b")
    elif percent_to_int(a) == percent_to_int(b):
        print("a = b")
    else:
        print("输入有误,无法比较")

0 人点赞