Can';t使用随机数使Python程序工作
我正在尝试创建一个程序,它使用随机浮动来执行用户选择的操作。在当前设置中,无论我选择哪种操作,程序都会直接打印“退出。感谢您使用我的程序!”Can';t使用随机数使Python程序工作,python,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
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 ): ")
您正在if-else语句中定义函数,但从未调用它们,我将代码切换了一点,现在似乎可以工作了
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 ): ")
您正在if-else语句中定义函数,但从未调用它们,我将代码切换了一点,现在似乎可以工作了
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 ): ")
这段代码有几个问题 首先,您不调用任何函数。相反,您只是简单地定义了它们。例如,如果输入“+”,则将输入if条件,其中
op=='+'
。但是,由于您没有实际调用函数add2(a,b)
,因此不会执行任何操作。你只是简单地定义了它
其次,不将生成的随机值保存到变量中。而不是:
random.randint(1,10)
random.random()*10
你可能会这样写:
a = random.randint(1, 10)
b = random.random() * 10
第三,在定义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
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中可以使用的其他技巧,您可以进一步改进上述代码。此代码存在一些问题 首先,您不调用任何函数。相反,您只是简单地定义了它们。例如,如果输入“+”,则将输入if条件,其中
op=='+'
。但是,由于您没有实际调用函数add2(a,b)
,因此不会执行任何操作。你只是简单地定义了它
其次,不将生成的随机值保存到变量中。而不是:
random.randint(1,10)
random.random()*10
你可能会这样写:
a = random.randint(1, 10)
b = random.random() * 10
第三,在定义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
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中可以使用的其他技巧,您可以进一步改进上述代码。您应该调用函数来使用它。试试这个:
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
你应该调用函数来使用它。试试这个:
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
是否要为任何操作生成新编号?是否要为任何操作生成新编号?