如果他们提交的东西比他们收到的东西高,我该如何退出行动

时间:2019-02-17 14:38:57

标签: python

我正在建立薪酬系统,如果他们的信用额度低于他们向某人付款的额度,我需要帮助退出该行动

这是一个付费系统,我已经尝试过制作一条新线并执行此操作,但我没有其他任何保留意见。我是python的新手,是的...

credits = 100
person = (input("Who do you want to pay?"))
numberpay = int(input("How much do you want to pay {person}?".format(person=person)))
accept = (input("Are you sure you want to pay {person} {numberpay} credits, Yes or no?".format(person=person, numberpay=numberpay)))
if accept.lower() == 'no':
    print("Your action to pay {person} {numberpay} credits was cancelled.".format(person=person, numberpay=numberpay))
if accept.lower() == 'yes':
    print("{numberpay} credits was taken from your account and given to {person}.".format(numberpay=numberpay, person=person))
    if numberpay ^ credits:
        print("Your action was cancelled due to a loss of credits.")
    totbal = ((credits) - (numberpay))
    print("Your current balance is now {totbal}.".format(totbal=totbal))

我希望它说“您的操作由于信用损失而被取消。”当他们没有足够的学分时,但这是行不通的。即使我有足够的学分可以放弃,它也只是说出来。

2 个答案:

答案 0 :(得分:0)

两项立即更改:使用正确的运算符,并确保totbal = ((credits) - (numberpay))仅在有足够资金时才执行。

^是按位异或。您应该更改

if numberpay ^ credits:

if numberpay - credits <= 0:

您需要包含else语句来处理信用减少和打印消息:

    if numberpay - credits <= 0:
        print("Your action was cancelled due to a loss of credits.")
    else:
        # reduce the balance
        credits -= numberpay
        print("Your current balance is now {credits}.".format(credits=credits))

如果您使用的是python3.6或更高版本,则可以使用fstrings简化字符串。 例如:

numberpay = int(input("How much do you want to pay {person}?".format(person=person)))
accept = (input("Are you sure you want to pay {person} {numberpay} credits, Yes or no?".format(person=person, numberpay=numberpay)))

将成为

numberpay = int(input(f"How much do you want to pay {person}?"))
accept = (input(v"Are you sure you want to pay {person} {numberpay} credits, Yes or no?"))

答案 1 :(得分:0)

欢迎来到我们的社区!

这是正确的方法(使用您自己的代码):

if accept.lower() == 'yes':
    print("{numberpay} credits was taken from your account and given to {person}.".format(numberpay=numberpay, person=person))
    if numberpay > credits:
        print("Your action was cancelled due to a loss of credits.")
    else:
        totbal = ((credits) - (numberpay))
    print("Your current balance is now {totbal}.".format(totbal=totbal))

请使用>(大于此值)。

在代码中,即使numberpay大于credits,您还是从numberpay中减去credits,这毫无意义。请看看我的操作方式...

祝你好运!