如何在Python中随机选择变量?

如何在Python中随机选择变量?,python,Python,如何从四个操作中随机选择一个,并针对每个问题进行更改?没有必要更改它,但我不想每次都显示相同类型的操作 要清除某些内容,question=input(“您想要什么类型的问题?(键入1/2/3/4):” 我写这行代码只是为了测试代码是否正常工作。您可以使用random.randint from random import* from time import* print("1. Addition \n2. Subtraction \n3. Multiplication \n4. Divi

如何从四个操作中随机选择一个,并针对每个问题进行更改?没有必要更改它,但我不想每次都显示相同类型的操作

要清除某些内容,
question=input(“您想要什么类型的问题?(键入1/2/3/4):”

我写这行代码只是为了测试代码是否正常工作。

您可以使用
random.randint

from random import*
from time import*
print("1. Addition \n2. Subtraction \n3. Multiplication \n4. Division")
count = int(input("How many question do you want? : "))
question = input("What kind of questions do you want? (Type 1/2/3/4) :")
mark = 0
if question == "1": 
    for x in range(count) :
        a = randint(1,100)
        b = randint(1,500)
        question = input("What is the value of {} + {} : ".format(a,b))               
        if int(question) == (a + b):
            print("You are correct")
            mark = mark+1
        else:
            print("Your answer is wrong")
            print("The correct answer is {}".format(a+b))
if question == "2":
    for x in range(count) : 
        a = randint(1,100)
        b = randint(1,500) 
        if b > a:
            a,b = b,a
            question = input("What is the value of {} - {} : ".format(a,b))
            if int(question) == (a - b): 
                print("You are correct")
                mark = mark + 1
            elif int(question) != (a - b):
                print("Your answer is wrong")
                print("The correct answer is {}".format(a-b))
                mark = mark + 0
elif question == "3":
    for m in range(count) : 
        a = randint(1,100)
        b = randint(1,500) 
        question = input("What is the value of {} ⨯ {} : ".format(a,b))               
        if int(question) == (a * b): 
            print("You are correct")
            mark = mark + 1
        elif int(question) != (a*b):
            print("Your answer is wrong")
            print("The correct answer is {}".format(a*b))
            mark = mark + 0
elif question == "4":
    for m in range(count) : 
        a = randint(1,100)
        b = randint(1,500)
        if b > a:
            a,b = b,a
            question = input("What is the value of {} ÷ {} in integer: ".format(a,b))               
            if int(question) == (a // b): 
                print("You are correct")
            elif int(question) != (a//b):
                print("Your answer is wrong")
                print("The correct answer is {}".format(a//b))  
        
sleep(2)
print("\nYour final mark is {}".format(mark))

您可以使用随机库并传递列表。它将从列表中输出一个随机项

import random

question = random.randint(1, 4)   # Returns an integer value between 1 and 4

if question == 1:
    # Do Something
elif question == 2:
    # Do Something, etc.

random.choice()?
欢迎使用堆栈溢出。choice()方法(来自随机库)接收一个iterable,它将在其中随机检索其中一个。您需要确切的代码示例吗?对于将来的扩展,考虑一种不同的方法。将每个交换置于其自身的功能中。然后列出函数,并使用
random.choice
选择其中一个函数。
import random
random.choice([1,2,3,4])