简体   繁体   English

从1个单一功能分配5个不同的变量

[英]Assigning 5 different variables from 1 single function

I have function scoring that I have to use to create 5 variables, which are randomly selected with integers, with scores1 being the first variable. 我必须使用函数评分来创建5个变量,这些变量是用整数随机选择的,其中scores1是第一个变量。 I want to be able to assign new variables scores2 , scores3 , scores4 , and scores5 , which all follow the same path as scores1 . 我希望能够分配新变量scores2,scores3,scores4scores5,所有遵循scores1相同的路径。 The code below does not have errors; 下面的代码没有错误; it is just an example of what I am trying to explain. 这只是我要解释的一个例子。 How can you use or edit this function to create these 5 random integer variables? 如何使用或编辑此函数来创建这5个随机整数变量?

import simplegui

import random

A_plus = 3
A = 3
B = 36
C = 58

def scoring():
    global scores

    score = random.randint(1, 100)
    if score < 4:

        scores = ['97', '98', '99', '100']
        scores1 = random.choice(scores)


    if 3 < score < 7:

        scores = ['93','94','95','96'] 
        scores1 = random.choice(scores)


    if 6 < score < 45:

        scores = ['83','86','87','89']
        scores1 = random.choice(scores)


    if 44 < score < 101:

        scores = ['70','72','76','79']
        scores1 = random.choice(scores)


scoring()

The most straight-forward approach is to make the assignments outside of the function. 最简单的方法是在函数外部进行赋值。 Then it is easy to put the results in any required variable: 然后,很容易将结果放入任何必需的变量中:

def scoring():
    score = random.randint(1, 100)
    if score < 4:
        scores = ['97', '98', '99', '100']
        score = random.choice(scores)
    if 3 < score < 7:
        scores = ['93','94','95','96'] 
        score = random.choice(scores)
    if 6 < score < 45:
        scores = ['83','86','87','89']
        score = random.choice(scores)
    if 44 < score < 101:
        scores = ['70','72','76','79']
        score = random.choice(scores)
    return score

score1 = scoring()
score2 = scoring()
score3 = scoring()
score4 = scoring()
score5 = scoring()

I don't know what you are trying to do, but below code will do the work for you 我不知道您要做什么,但是下面的代码将为您完成工作

import random

A_plus = 3
A = 3
B = 36
C = 58

def scoring():
    global scores

    score = random.randint(1, 100)
    if score < 4:

        scores = ['97', '98', '99', '100']
        scores1 = random.choice(scores)


    if 3 < score < 7:

        scores = ['93','94','95','96'] 
        scores1 = random.choice(scores)


    if 6 < score < 45:

        scores = ['83','86','87','89']
        scores1 = random.choice(scores)


    if 44 < score < 101:

        scores = ['70','72','76','79']
        scores1 = random.choice(scores)

    return scores1
score_list = []
for each in range(5):
    score_list.append(scoring())
print score_list

output : ['87', '79', '79', '89', '86']

You just need to bind it to a variable. 您只需要将其绑定到变量即可。 Its up to you, you can put it in a list or a dictionary or individual variables 由您自己决定,您可以将其放在列表或字典中或单个变量中

I think this is what you want: 我认为这是您想要的:

import random

def scoring(score):
    if 0 <= score < 4:
        scores = ['97', '98', '99', '100']
        return random.choice(scores)
    elif 4 <= score < 7:
        scores = ['93','94','95','96'] 
        return random.choice(scores)
    elif 7 <= score < 45:
        scores = ['83','86','87','89']
        return random.choice(scores)
    elif 45 <= score <= 100:
        scores = ['70','72','76','79']
        return random.choice(scores)

l = [3, 3, 57, 70, 99] # these are the list of variables
scores = [scoring(score) for score in l]
print(scores)

Output: 输出:

['100', '97', '89', '72']

Since the output is randomised, the output above is for your reference. 由于输出是随机的,因此以上输出仅供参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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