如果/ else阻止,Python脚本失败运行

时间:2015-12-08 21:05:07

标签: python

运行以下命令,只运行menu()函数,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

2 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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
'''