CS50P 作业 Problem Set 3

Fuel Gauge

def main():
    p = get_percent()

    if (p <= 0.01):
        print("E")
    elif(p >= 0.99):
        print("F")
    else:
        per = "{:.2f}".format(p)
        percent = int(float(per) * 100)
        print(f"{percent}%")


def get_percent():
    while True:
        try:
            user_input = input("Fraction: ")
            x, y = user_input.split('/')
            x = int(x)
            y = int(y)
            if x > y:
                continue
        except ValueError or ZeroDivisionError:
            pass
        else:
            break
    p = x / y
    return p


main()

Felipe’s Taqueria

def main():
    total = 0
    food_list = {"Baja Taco": 4.25,"Burrito": 7.50,"Bowl": 8.50,"Nachos": 11.00,"Quesadilla": 8.50,"Super Burrito": 8.50,"Super Quesadilla": 9.50,"Taco": 3.00,"Tortilla Salad": 8.00
}
    while True:
        try:
            user_input = input("Item: ").title()
            if user_input in food_list:
                total = total + food_list[user_input]
                t = "{:.2f}".format(total)
                print(f"Total: ${t}")
                continue
            else:
                continue
        except ValueError:
            pass
        except EOFError:
            break

main()

Grocery List

def main():
    user_list = sorted(addlist(),key =str.lower)
    new_list = []
    for i in user_list:
        if i not in new_list:
            print(f"{user_list.count(i)} {i}")
            new_list.append(i)

def addlist():
    user_list = []
    while True:
        try:
            user_input = input("").upper()
            user_list.append(user_input)
        except ValueError:
            pass
        except EOFError:
            break
        else:
            pass
    return user_list
main()

Outdated

def main():

    use_list = [
    "January",
    "February",
    "March",
    "April",
    "May",
    "June",
    "July",
    "August",
    "September",
    "October",
    "November",
    "December"
]
    while True:
        try:
            user_input = input("Date: ")
            if "/" in user_input:
                a, b,c= user_input.split('/')
                yyyy = int(c)
                mm = int(a)
                dd = int(b)

            elif "," in user_input:
                x , y = user_input.split(',')

                yue,ri = x.split(' ')
                if yue in use_list:
                    mm = use_list.index(yue) + 1
                    dd = ri
                    yyyy = y.lstrip()
                else:
                    continue
            else:
                continue
        except (NameError, ValueError, TypeError):
            pass
        else:
            if int(dd)<= 31 and 1<=int(mm)<=12:
                break
    mm = str(mm).strip()
    dd = str(dd).lstrip()
    yyyy = str(yyyy).lstrip()
    if int(mm)<10:
        mm= "0"+mm
    if int(dd) <10:
        dd ="0"+dd
    print(f"{yyyy}-{mm}-{dd}")
main()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值