當我執行並運行代碼時,程序似乎不存儲c輸入,因此不會繼續執行代碼來執行計算器功能的其餘部分。爲什麼我的Python計算器不工作?
def calc():
print("Press 1 for addition")
print("Press 2 for subtraction")
print("Press 3 for multiplication")
print("Press 4 for division")
c = input()
if c == 1:
print("Enter a number")
x = input()
print("Enter another number")
y = input()
return x + y
elif c == 2:
print("Enter a number")
x = input()
print("Enter another number")
y = input()
return x - y
elif c == 3:
print("Enter a number")
x = input()
print("Enter another number")
y = input()
return x * y
elif c == 4:
print("Enter a number")
x = input()
print("Enter another number")
y = input()
return x/y
calc()
我現在已經改進的代碼,但似乎無法得到正確的縮進,似乎是對每種類型的正在執行的數學回歸函數是「外面的功能」
def calc():
print("Press 1 for addition")
print("Press 2 for subtraction")
print("Press 3 for multiplication")
print("Press 4 for division")
c = int(input())
def get_inputs():
print("Enter a number")
x = int(input())
print("Enter another number")
y = int(input())
return x, y
if c == 1:
x, y = get_inputs()
return x + y #These return functions seem to be an error
elif c == 2:
x, y = get_inputs()
return x - y
elif c == 3:
x, y = get_inputs()
return x * y
elif c == 4:
x, y = get_inputs()
return x/y
calc()
'輸入()'返回在Python 3的字符串;你想要一個整數'c = int(input())' –
是什麼問題? – harshil9968