簡體   English   中英

在 Python 中制作一個簡單的計算器,但不確定如何為其他人做任何事情

[英]Making a simple calculator in Python and not sure how to do nothing for else

我正在嘗試制作一個簡單地添加或減去 2 個數字的程序。 我有下面的代碼:

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or "A":
    x + y == z 
    if txt == "s" or "S":
        x - y == z 
    else: 
        return
else:
    return
print (z)
    

我知道回報不正確,但不確定我應該如何解決這個問題。

首先,當你寫:

x + y == z

您不是在定義一個新的 z 變量,而是在進行邏輯運算“x + y 是否等於 z?” 其中 z 甚至沒有定義。 如果要將 z 定義為 x 和 y 的和或差,則應使用:

z = x + yz = x - y

另外,當你想創建一個“if condition is equal to something, else if condition is equal to something else”這樣的結構時,你可以使用 if、else 和 elif(它既是 else 又是 if ),但是你有以確保它們具有相同的縮進。

以下代碼應該可以完成這項工作:(已編輯)

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or txt=="A":
    z = x + y 
elif txt == "s" or txt=="S":
    z = x - y 
print (z)

編輯:您沒有定義函數,因此您不需要使用“返回”。 Return 用於給出函數的結果,例如:

def sum(x, y):
    z = x + y 
    return(z)

編輯 #2:謝謝你讓我注意到txt == 'a' or 'A'總是正確的,我現在已經修復了代碼。

首先: x + y == z是對相等性的測試。 它檢查x + yz是否相等。 要將x + y的結果分配z ,您需要執行z = x + y

對於此類問題,繪制流程圖或至少寫出您將在代碼中實現的步驟確實很有幫助,尤其是在您開始編程時。 這是一個例子。 注意我的編號列表的縮進。 這類似於您希望在 Python 代碼中看到的縮進。

  1. 取兩個數
    • input()返回一個字符串,因此轉換為整數
  2. 詢問運營商
    • input()取一個字符串。
  3. 如果運算符是“a”或“A”(見注1)
    • 做加法
  4. 相反,如果運算符是“s”或“S”
    • 做減法
  5. 如果操作者不是上述任何一種
    • 沒做什么? 顯示錯誤?
  6. 最后,打印輸出

我們上面討論的算法的代碼是:

x = int(input("Enter a number: ")) # 1.
y = int(input("Enter a number: ")) # 1.
print("Would you like to add or subtract?") # 2.
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or txt == "A": # 3.
    z = x + y
elif txt == "s" or txt == "S": # 4.
    z = x - y
else: # 5.
    z = 0
    print("Invalid choice!")

print (z) # 6.

您的代碼在這里出錯:

  1. 如果運算符是“a”或“A”
    • 做加法
    1. 現在,如果運算符是“s”或“S” (它可以是“s”還是“S”?,因為我們已經在上面的3.確定它是“a”或“A”)
      • 做減法

注 1 :在人類語言中,我們說

檢查txt是“a”還是“A”

但是 Python 在if條件下使用布爾值。 如果我們這樣做if txt == "a" or "A" ,這將評估為if [[(txt == "a") is true] or ["A" is true]] 因為非空字符串在 Python 中["A" is true]["A" is true]總是正確的,所以[[(txt == "a") is true] or ["A" is true]]總是true ,所以我們總是會進入if語句。 不是我們想要發生的! 我們必須像這樣分別比較每一個:

if txt == "a" or txt == "A":

或者,我們可以將txt轉換為小寫或大寫並進行一次檢查

if txt.lower() == "a":

或者,

if txt.upper() == "A":

或者,我們可以檢查txt是否是列表中的元素之一。

if txt in ["a", "A"]:

你需要做z = x + yz = x - y而不是x + y == zx - y == z 雙等於是一個比較運算符。

下面的代碼應該工作:

x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
z = 0
print("Would you like to add or subtract?")
txt = input("Type 'a' for add or 's' for subtract")
if txt == "a" or "A":
    z = x + y
elif txt == "s" or "S":
    z = x - y
//you can use return if it's a function
return z;   
print (z)

暫無
暫無

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

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