2015-12-08 101 views
-1

運行以下操作,只運行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

+0

輸入返回一個字符串,但你想一個int,所以你必須在比較之前轉換。嘗試'choice = int(輸入('從菜單中選擇一個數字'))' – Wolf

+1

每個函數中'return'語句的用途是什麼?每個函數下面的代碼都不起作用。這是整個代碼還是更多?因爲您粘貼的代碼段中沒有定義'num14'。 – Jarwin

+0

同樣在一個不相關的說明中,所有'import'語句都寫在頂部。 – Jarwin

回答

0

input()返回一個字符串,但所有的if語句檢查一個整數。因此,條件永遠不會是真實的,例如字符串"1"不等於整數1

變化

choice = input('pick a number from the menu') 

choice = int(input('pick a number from the menu')) 

,你應該取得一些進展。

完成更改後,可能需要添加一些錯誤檢查以處理用戶輸入無法轉換爲有效整數的字符串的情況。您可以使用try/except這樣的:

try: 
    choice = input('pick a number from the menu') 
    choice = int(choice) 
    ... # rest of your code goes here 
except ValueError: 
    print("Invalid choice {!r} made".format(choice)) 
+0

我試過了,它沒有工作 – 13ostiil

+0

它以什麼方式不起作用?你需要解釋什麼是錯的。 – mhawke

+0

我已經成功完成該程序。 – 13ostiil

0

以下是完整的程序:

def menu(): 
print(''' 
******************** 
* 1-addition  * 
* 2-subtraction * 
* 3-multiplication * 
* 4-devision  * 
* 5-quit   * 
******************** 
''') 


import time 
import random 



def multiplication_random(number14,number24): 
    c=number14*number24 
    return c 
    print("these are going to be multiplied, they are random numbers:") 




def devision_random(number1,number2): 
    c=number1/number2 
    return c 
    print("these are going to be devided, they are random numbers:") 



def subtract_random(number13,number23): 
    c=number13-number23 
    return c 
    print("these are going to be subtracted, they are random numbers:") 



def add_random(number11,number21): 
    c=number11+number21 
    return c 
    print("these are going to be added, they are random numbers:") 




menu() 
choice = int(input('pick a number from the menu')) 

if choice == 1: 
    number11 = random.randint (1,1000) 
    number21 = random.randint (1,1000) 
    print(number11,'+',number21) 
    time.sleep(0.5) 
    print('calculating...') 
    time.sleep(0.5) 
    print(add_random(number11,number21)) 

elif choice == 2: 
    number13 = random.randint (1,1000) 
    number23 = random.randint (1,1000) 
    print(number13,'-',number23) 
    time.sleep(0.5) 
    print('calculating...') 
    time.sleep(0.5) 
    print(subtract_random(number13,number23)) 


elif choice == 3: 
    number14 = random.randint (1,1000) 
    number24 = random.randint (1,1000) 
    print(number14,'*',number24) 
    time.sleep(0.5) 
    print('calculating...') 
    time.sleep(0.5) 
    print(multiplication_random(number14,number24)) 

elif choice == 4: 
    number2 =random.randint (1,1000) 
    number1 = number2 * random.randint (1,1000) 
    print(number1,'/',number2) 
    time.sleep(0.5) 
    print('calculating...') 
    time.sleep(0.5) 
    print(devision_random(number1,number2)) 

elif choice == 5: 
    print('Goodbye') 
    print('  z') 
    print('  z') 
    print('  z') 
    print(' z') 
    print(' z') 
    print(' 0') 
    print('/|\\') 
    print('/ \\') 







''' 
Thank you for your help 
'''