簡體   English   中英

當用戶輸入 y 時,如何使程序重新運行?

[英]How Can I make the program rerun when the user inputs y?

print("Welcome to The pyCalc")
print("For Addition press 1")
print("For Multiplication press 2")
print("For Subtraction press 3")
print("For Division press 4")
flag = 'y'
while flag == 'y':
    x = float(input("Enter your Choice(1-4): "))
    if x == 1:
        a = float(input("Enter 1st Value: "))
        b = float(input("enter 2nd Value: "))
        c = a+b
        print("Sum: ", int(c))
    elif x == 2:
        a = float(input("Enter 1st Value: "))    
        b = float(input("Enter 2nd Value: "))
        c = a*b 
        print("Product: ", int(c))
    elif x == 3:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a - b
        print("Difference: ", int(c))
    elif x == 4:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a // b
        print("Quotient: ", c)
    else:
        print("error !")
        flag = print(input("Do you want to calculate more(y/n)? : ", y))
    if flag == y:
        continue    

嗨,我實際上是 Python 的新手,所以對基本概念都不太熟悉。 所以在這個計算器程序中,第一次運行后,我希望代碼在用戶輸入 y 時重新運行。 據我所知,這可以使用 while 循環來實現,在該循環中,我們將主代碼放在其主體中,並使用 if 語句給出條件,其次使用 continue 語句。 有人可以解釋這里出了什么問題。 ps 縮進在某些情況下可能是錯誤的。

我重寫了你的代碼,因為有兩個問題:

  1. 沒有print(input(""))這樣的東西
  2. flag = input("Do you want to calculate more(y/n)? : ")應該在 else 之外
print("Welcome to The pyCalc")
print("For Addition press 1")
print("For Multiplication press 2")
print("For Subtraction press 3")
print("For Division press 4")
flag = 'y'
while flag == 'y':
    x = float(input("Enter your Choice(1-4): "))
    if x == 1:
        a = float(input("Enter 1st Value: "))
        b = float(input("enter 2nd Value: "))
        c = a+b
        print("Sum: ", int(c))
    elif x == 2:
        a = float(input("Enter 1st Value: "))    
        b = float(input("Enter 2nd Value: "))
        c = a*b 
        print("Product: ", int(c))
    elif x == 3:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a - b
        print("Difference: ", int(c))
    elif x == 4:
        a = float(input("Enter the 1st value: "))
        b = float(input("Enter the 2nd value: "))
        c = a // b
        print("Quotient: ", c)
    else:
        print("error !")
    flag = input("Do you want to calculate more(y/n)? : ")
    if flag.lower() == "y":
        continue 


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM