无法使Python程序使用随机数工作

时间:2019-03-03 22:30:39

标签: python

我正在尝试创建一个使用随机浮点数执行用户选择的操作的程序。在当前设置中,程序直接打印“退出。谢谢您使用我的程序!”无论我选择哪种操作。

print ( "This program performs mathematical functions with random numbers." )

op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

import random
random.randint(1,10) 
random.random()*10



while (op != "q"): 
    if (op == "+"):
        def add2(a,b):
            print ("mathop called with",a,"and",b)
            c=a+b
            return c
    elif (op == "-"):
            def subtract2(a,b):
                print ("mathop called with",a,"and",b)
                c=a-b
                return c
    elif (op == "*"):
            def multiply2(a,b):
                print ("mathop called with",a,"and",b)
                c=a+b
                return c
    elif (op == "/"):  
           def divide2(a,b):
               print ("mathop called with",a,"and",b)
               c=a/b
               return c
    elif (op == "%"):
            def remainder2(a,b):
                print ("mathop called with",a,"and",b)
                c=a%b
                return c
    else:
        print ("Quitting. Thank you for using my program!")
    op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

3 个答案:

答案 0 :(得分:2)

您正在if语句中定义函数,但从未调用它们,我将您的代码切换了一下,它现在似乎可以正常工作。

import random

def add2(a,b):
    print("mathop called with",a,"and",b)
    c=a+b
    return c

def subtract2(a,b):
    print("mathop called with",a,"and",b)
    c=a-b
    return c

def multiply2(a,b):
    print("mathop called with",a,"and",b)
    c=a*b
    return c

def divide2(a,b):
    print("mathop called with",a,"and",b)
    c=a/b
    return c

def remainder2(a,b):
    print("mathop called with",a,"and",b)
    c=a%b
    return c

print("This program performs mathematical functions with random numbers.")
op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

a = random.randint(1,10) 
b = random.random()*10

while (op != "q"): 
    if (op == "+"):
        print(add2(a,b))
    elif (op == "-"):
        print(subtract2(a,b))
    elif (op == "*"):
        print(multiply2(a,b))
    elif (op == "/"):  
        print(divide2(a,b))
    elif (op == "%"):
        print(remainder2(a,b))
    else:
        print("Quitting. Thank you for using my program!")
    op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

答案 1 :(得分:0)

此代码存在多个问题。

首先,您不调用任何函数。相反,您只需定义它们。例如,如果输入“ +”,则将在op == '+'处输入if条件。但是,由于您实际上并未调用函数add2(a,b),因此不会执行任何操作。您只需定义它即可。

第二,您不将生成的随机值保存到变量中。代替:

random.randint(1,10) 
random.random()*10

您可能可以这样写:

a = random.randint(1, 10)
b = random.random() * 10

第三,定义条件时无需使用方括号。

稍作修改后,您可能会编写类似以下代码的内容:

import random


def add2(a, b):
    print("mathop called with", a, "and", b)
    c = a + b
    return c


def subtract2(a, b):
    print("mathop called with", a, "and", b)
    c = a - b
    return c


def multiply2(a, b):
    print("mathop called with", a, "and", b)
    c = a + b
    return c


def divide2(a, b):
    print("mathop called with", a, "and", b)
    c = a / b
    return c


def remainder2(a, b):
    print("mathop called with", a, "and", b)
    c = a % b
    return c


op = None

while op != "q":
    op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

    a = random.randint(1, 10)
    b = random.random() * 10

    if op == "+":
        print(add2(a, b))
    elif op == "-":
        print(add2(a, b))
    elif op == "*":
        print(add2(a, b))
    elif op == "/":
        print(add2(a, b))
    elif op == "%":
        print(add2(a, b))
    else:
        print("Quitting. Thank you for using my program!")

通过消除if条件并替换为词典,可以进一步改善给定的代码。这样的事情会起作用:

import random


def add2(a, b):
    print("mathop called with", a, "and", b)
    c = a + b
    return c


def subtract2(a, b):
    print("mathop called with", a, "and", b)
    c = a - b
    return c


def multiply2(a, b):
    print("mathop called with", a, "and", b)
    c = a + b
    return c


def divide2(a, b):
    print("mathop called with", a, "and", b)
    c = a / b
    return c


def remainder2(a, b):
    print("mathop called with", a, "and", b)
    c = a % b
    return c


op = None

operations = {'+': add2, '-': subtract2, '*': multiply2, '/': divide2, '%': remainder2}

while op != "q":
    op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

    a = random.randint(1, 10)
    b = random.random() * 10

    if op in operations:
        operations[op](a, b)

print("Quitting. Thank you for using my program!")

您可以通过探索可以在python中完成的其他技巧来进一步改进上面的代码。

答案 2 :(得分:0)

您应该调用该函数以使用它。试试这个:

import random

def add2(a,b):
    print ("mathop called with",a,"and",b)
    c=a+b
    return c

def subtract2(a,b):
    print ("mathop called with",a,"and",b)
    c=a-b
    return c

def multiply2(a,b):
    print ("mathop called with",a,"and",b)
    c=a*b
    return c

def divide2(a,b):
    print ("mathop called with",a,"and",b)
    c=a/b
    return c

def remainder2(a,b):
    print ("mathop called with",a,"and",b)
    c=a%b
    return c

print ( "This program performs mathematical functions with random numbers." )

p = random.randint(1,10) 
q = random.random()*10

while True:

    op = input("Choose a math operation (Please type +, -, *, /, % to select an operation, or type q to quit ): ")

    if (op == "+"):        
        print(add2(p, q))

    elif (op == "-"):            
            print(subtract2(p, q))

    elif (op == "*"):            
            print(multiply2(p,q))

    elif (op == "/"):             
           print(divide2(p, q))

    elif (op == "%"):            
            print(remainder2(p, q))

    elif (op == 'q'):
        print ("Quitting. Thank you for using my program!")
        break