所以我對 Python 還是很陌生,而且很感興趣。現在,我正在嘗試撰寫一個腳本來制作一個漂亮的計算器。我有計算器的底座,它作業得很好。現在,我只希望它詢問用戶是否愿意進行另一次計算。從我的想法來看,它可能是這樣的:
- 第一個數字輸入
- 第二個數字輸入
- 計算操作
- 結果
- 你想重新開始嗎?
到目前為止,我有這個:
first_number = input("First number: ")
second_number = input("Second number: ")
operation = input("(M)ultiply (D)ivide (S)ubtract (A)dd: ")
if operation.upper() == "M":
outcome = int(first_number) * int(second_number)
print(outcome)
elif operation.upper() == "D":
outcome = int(first_number) / int(second_number)
print(outcome)
elif operation.upper() == "S":
outcome = int(first_number) - int(second_number)
print(outcome)
elif operation.upper() == "A":
outcome = int(first_number) int(second_number)
print(outcome)
有人可以幫我在主要部分之后問新的計算問題嗎?
uj5u.com熱心網友回復:
你可以用一段時間來處理這種情況。
operation = ""
while operation.upper() != "Q":
first_number = input("First number: ")
second_number = input("Second number: ")
operation = input("(M)ultiply (D)ivide (S)ubtract (A)dd (Q)uit:")
if operation.upper() == "M":
# ...
elif operation.upper() == "A":
# ...
如果鍵入“Q”,在 if/elif 塊的末尾(在這種情況下始終為 false),代碼將退出回圈,因為條件operation.upper() != "Q"為 False 并結束程式。這是一目了然的邏輯。當您詢問用戶是否希望重復計算時,您可以修改腳本以添加額外的列印。
uj5u.com熱心網友回復:
所以一個很好的方法是添加一個while回圈來檢查我們稱之為“runnextcalculation”的變數是否為真。因此,我們將其初始化為 true(我們肯定要運行我們的第一個計算)然后我們將按照您的代碼中的描述收集輸入。然后,最后,我們添加一個新的輸入欄位,詢問用戶是否要運行另一個計算。如果這個答案是“N”(否),我們將 runnextcalculation 設定為 false。當我們回到 while 陳述句的頂部時,runnextcalculation 將強制 while 回圈終止,程式將完成。如果是 Y,行程將重新啟動。這是代碼:
runnextcalculation = True
while runnextcalculation is True:
first_number = input("First number: ")
second_number = input("Second number: ")
operation = input("(M)ultiply (D)ivide (S)ubtract (A)dd: ")
if operation.upper() == "M":
outcome = int(first_number) * int(second_number)
print(outcome)
elif operation.upper() == "D":
outcome = int(first_number) / int(second_number)
print(outcome)
elif operation.upper() == "S":
outcome = int(first_number) - int(second_number)
print(outcome)
elif operation.upper() == "A":
outcome = int(first_number) int(second_number)
print(outcome)
ask_if_run_next_calculation = input("Would you like to run another calculation? (Y/N)")
if ask_if_run_next_calculation == "N":
runnextcalculation = False
哪個輸出:
First number: 2
Second number: 2
(M)ultiply (D)ivide (S)ubtract (A)dd: A
4
Would you like to run another calculation? (Y/N)Y
First number: 2
Second number: 2
(M)ultiply (D)ivide (S)ubtract (A)dd: M
4
Would you like to run another calculation? (Y/N)N
Process finished with exit code 0
uj5u.com熱心網友回復:
您可以像這樣將整個腳本嵌套在 while 回圈中。
計算完成后,詢問用戶是否要停止。當他想退出時切換一個標志。
cont = True
while cont:
first_number = input("First number: ")
second_number = input("Second number: ")
operation = input("(M)ultiply (D)ivide (S)ubtract (A)dd :")
if operation.upper() == "M":
outcome = int(first_number) * int(second_number)
print(outcome)
elif operation.upper() == "D":
outcome = int(first_number) / int(second_number)
print(outcome)
elif operation.upper() == "S":
outcome = int(first_number) - int(second_number)
print(outcome)
elif operation.upper() == "A":
outcome = int(first_number) int(second_number)
print(outcome)
input_continue = input("(S)top :")
if input_continue.upper() == "S":
cont = False
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/411459.html
標籤:
