2

私はpython 2.75を使用しており、私の仕事はカフェの領収書を印刷することですが、小数点以下1桁まで印刷されるため、価格を通貨値で小数点以下2桁まで印刷する方法がわかりません。私は本当にpythonが初めてなので、誰かが助けてくれたら大歓迎です!

def main():
print("Welcome to Kenny's Drinking Cafe")
print("Here is the electronic menu: ")
print("Please select by typing the selections from the list below. *CaSe InSeNsItIvE*")
print("Spelling counts though ;)")
print "-------------------------------------------------------------------------------"
print("Coffee   $2.00")
print("Hot Chocolate   $1.50")
print("Latte   $3.00")
print("Frappuccino   $3.00")
print("Smoothie   $3.50")
countcoffee = 0
counthotc = 0
countlatte = 0
countfrap = 0
countsmoothie = 0 
coffeec = 2.00
hotcc = 1.50
lattec = 3.00
frapc = 3.00
smoothiec = 3.50

order1 = raw_input("Please enter your selection: Press Enter to Print receipt ")
while order1 != "":
    order1 = order1.lower()
    if order1 == str("coffee"):
        countcoffee = countcoffee + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 in [str("hot chocolate"), str("hotchocolate")]:
        counthotc = counthotc + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("latte"):
        countlatte = countlatte + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("frappuccino"):
        countfrap = countfrap + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    elif order1 == str("smoothie"):
        countsmoothie = countsmoothie + 1
        order1 = raw_input("Do you want to order more? Select: Coffee, Hot Chocolate, Latte, Frappuccino, Smoothies. Press Enter to Print receipt ")
    else:
        print "Please type correctly"
        exit()

coffee = (countcoffee*coffeec)
hotchocolate = (counthotc*hotcc)
latte = (countlatte*lattec)
frappuccino = (countfrap*frapc)
smoothie = (countsmoothie*smoothiec)
sum = (countcoffee*coffeec) + (counthotc*hotcc) + (countlatte*lattec) + (countfrap*frapc) + (countsmoothie*smoothiec)
print ""
print "Kenny's Drinking Cafe Receipt"
print ""
print "***********************"
print "**   Regular Items   **"
print "***********************"
if coffee > 0:
    print (str(countcoffee) + ' coffee ' + "@ $" + str(coffee))
if hotchocolate > 0:
    print counthotc,"hot chocolate","@ $",hotchocolate
if latte > 0:
    print countlatte,"latte", "@ $",latte
if frappuccino > 0:
    print countfrap,"frappuccino","@ $",frappuccino
if smoothie > 0:
    print countsmoothie,"smoothie","@ $",smoothie
print ("BALANCE: $" + str(sum))

main()
4

2 に答える 2

5

これは役立つはずです

print("BALANCE: $%.2f" % sum)

演算子は、文字列内の%指定されたパラメーターを置き換えます。また、.2f必要な形式を指定します。

文字列の書式設定について詳しくは、 docsを参照してください。

これを行うよりPythonstr.format()的な方法は、@sortfiendで指定されたように使用することです

print("BALANCE: {:.2f}".format(sum))
于 2013-11-05T04:40:44.907 に答える
2

float を出力するために、printf スタイルのフォーマットを使用できます。

>>> "%.2f" % 1
'1.00'
>>> "%.2f" % 1.0
'1.00'
>>> "%.2f" % 1.03
'1.03'
>>> "%.2f" % 1.034
'1.03'

同じ式を使用して変数に割り当てることができます。

>>> str = "%.2f" % 1.035
>>> str
'1.03'
于 2013-11-05T04:42:31.863 に答える