如果我输入$1,如何让python只计数?

2024-04-16 05:46:50 发布

您现在位置:Python中文网/ 问答频道 /正文

def bills():
 counts = 0
 while True:
   bills = int(input("Insert your bills:$"))
   if bills > 1:
    counts += 1
    print("Only accept $1 bill!")
    print(counts)
   elif bills == 0:
    break

如果账单是1美元,我只想算数。再加上总数。 谢谢


Tags: trueonlyinputyourifdefintinsert
1条回答
网友
1楼 · 发布于 2024-04-16 05:46:50

正如@Tim Pierce指出的,您只需要添加条件elif bills == 1

def bills():
    counts = 0
    while True:
        bills = int(input("Insert your bills:$"))

        if bills > 1:                #If input is greater than 1, print error and count
            print("Only accept $1 bill!")
            print(counts) 
        elif bills ==1:              #If input equals 1, increment and then print count
            counts += 1
            print(counts)
        else:                        #If input is less than 1, exit function
            break
bills()

相关问题 更多 >