運行以下操作,只運行menu()函數,if/else塊都不起作用。Python腳本運行失敗if/else塊
def menu():
print('''
1-addition
2-subtraction
3-multiplication
4-devision
''')
import random
def multiplication_random(number1,number2):
c=number1*number2
return c
num11 = random.randint (1,1000)
num21 = random.randint (1,1000)
print("these are going to be multiplied, they are random numbers:")
def devision_random(number1,number2):
c=number1/number2
return c
num22 =random.randint (1,1000)
num12 = num2 * random.randint (1,1000)
print("these are going to be devided, they are random numbers:")
def subtract_random(number1,number2):
c=number1-number2
return c
num13 = random.randint (1,1000)
num23 = random.randint (1,1000)
print("these are going to be subtracted, they are random numbers:")
def add_random(number1,number2):
c=number1+number2
return c
num14 = random.randint (1,1000)
num24 = random.randint (1,1000)
print("these are going to be added, they are random numbers:")
menu()
choice = input('pick a number from the menu')
if choice == 1:
add_random(number1,number2)
print(num14)
print(num24)
print(add_random(num11,num21))
elif choice == 2:
subtract_random(number1,number2)
print(num13)
print(num23)
print(subtract_random(num12,num22))
elif choice == 3:
multiplication_random(number1,number2)
print(num11)
print(num21)
print(multiplication_random(num13,num23))
elif choice == 4:
devision_random(number1,number2)
print(num12)
print(num22)
print(devision_random(num14,num24))
我使用Python 3.5
輸入返回一個字符串,但你想一個int,所以你必須在比較之前轉換。嘗試'choice = int(輸入('從菜單中選擇一個數字'))' – Wolf
每個函數中'return'語句的用途是什麼?每個函數下面的代碼都不起作用。這是整個代碼還是更多?因爲您粘貼的代碼段中沒有定義'num14'。 – Jarwin
同樣在一個不相關的說明中,所有'import'語句都寫在頂部。 – Jarwin