简体   繁体   中英

Python script failing run if/else block

Running the following, only the menu() function is run, none of the if/else block seems to work.

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))

I am using python 3.5

input() returns a string but all of your if statements check for an integer. Therefore the conditions can never be true eg the string "1" is not equal to the integer 1 .

Change

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

to

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

and you should make some progress.

Once you have made the change, you might want to add some error checking to handle the case that the user enters a string that can not be converted to a valid integer. You can use try/except like this:

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))

Here is the full program:

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
'''

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM