Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

python - I've Created shop calculator in pyhton but it have some bugs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's because nothing happens after you enter the amounts. See here from the first if block. You call main() but then you've got nothing following it to ever print because where you print your total is in an else.

if m == "a":
  a_t = eval(input("How many ? "))
  a_p = apple * a_t
  total = a_p + total
  main() # nothing happens after this, it cannot enter the `else` block
else:
    total = str(total)
    print("You've purchased " + total + " dollar from our shop 
 Have a nice day !!!")

You're trying to do this recursively and although that can work you're probably better off with a while loop

Example for clarity:

total = 0
product = None
while product != "e":
    m = input("Which fruit u want to purchase? 
 press < a > for apple 
 press < o > for orange 
 press < b > for banana 
 press e for exit: ")
    number = eval(input("How many ? "))
    if m == "a":
        p = apple * number 
    elif m == "o":
        p = orange * number 
    total += p
    product = m # when this is "e" it will break the while loop
    
print("You've purchased " + total + " dollar from our shop 
 Have a nice day !!!") # will print after the while loop has finished

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...