在python中对变量进行更改

2024-10-05 14:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我正在尝试制作一个医院sim卡(基于文本),可以选择招聘更多员工。我已经想出了如何限制他们可以招聘的次数(限制为350名员工),他们得到的数量应该是随机的(random.choice(staffrec),但一旦staffrec生成了随机数,它就保持不变,我希望代码再次运行,并产生不同的结果。下面是我的代码。(抱歉这么长,我已经全部放好了,所以我没有遗漏任何内容)

import time
import random

staff = 100
wards = 20
beds = 140
patients = 80

staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]
staffrec = random.choice(staffrec)


option = ""

print("Welcome to Hospital Simulator!")

print("You have to manage the hospital with patients coming in,")
print("Staff recruitement and staff quitting,")
print("and how many beds and wards are available!")

Aopt = ["A", "a"]
Bopt = ["B", "b"]
Copt = ["C", "c"]
Dopt = ["D", "d"]
Eopt = ["E", "e"]
Fopt = ["F", "f"]

while staff <= 350:
    staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    staffrec = random.choice(staffrec)
    staff = staff + staffrec

while option != Fopt:
    option = input("""What would you like to do:
A: View number of patients
B: View number of beds
C: View number of wards
D: View number of staff
E: Recruit more staff
F: Quit\n""")

    if option in Aopt:
        print("You currently have " + str(patients) + " patients\n")

    if option in Bopt:
        print("You currently have " + str(beds) + " beds\n")

    if option in Copt:
        print("You have " + str(wards) + " wards\n")

    if option in Dopt:
        print("You currently have " + str(staff) + " staff\n")

    if option in Eopt:
        print("You try and recruit more staff. You recruit " + str(staffrec) + " staff\n")
        if staff > 350:
            print("You cannot recruit any more staff!")

    if option in Fopt:
        print("Goodbye!")
        break

也有其他文章使用类与此主题相关,但由于我对python相当陌生,所以我没有完全理解它们。3.如果您能提供任何帮助,我们将不胜感激! 彼得


Tags: andinyouviewifhaverandomoption
3条回答

欢迎来到论坛

关于您的代码,我有几点很突出:

staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]
staffrec = random.choice(staffrec)

这两行没有任何作用,因为您稍后将替换staffrec的值。此外,根据经验,避免重用变量名,特别是当您使用完全不同的类型重新分配变量名时(staffrec是一个列表,现在是一个整数)

此循环:

while staff <= 350:
    staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    staffrec = random.choice(staffrec)
    staff = staff + staffrec

看起来是多余的,实际上它并没有将staff限制为最大350。使用这种方法,当循环结束时,staff将始终在351和359之间。 您希望实际范围是多少?为什么你需要一个循环?只需使用random.randint在固定范围内生成一个数字

这种情况:

while option != Fopt:

永远不会是假的option将始终是字符串,并且字符串永远不会等于元组。由于您稍后使用break来打破循环,因此您最好将其转换为while True:

如果我正确理解了这个问题,那么您想知道如何为staff生成一个新值(通过从前面重新运行while循环)。您可以将其封装在函数中,但这似乎是不必要的—只需使用random.randint生成一个新值,就像我前面提到的那样

在主游戏逻辑之前分配staffrec的值,并且从不使用新的随机值更新,因此每次进入添加新成员的循环块时,其值都相同。如果希望每次都更改,只需在需要随机数时调用random

我还建议使用^{}方法进行选择,因为它将在任意两个值之间生成一个随机数。例如,random.randint(0, 100)将返回一个介于0和100之间的随机数

见下文:

if option in Eopt:
    staffrec = random.randint(1, 9)
    staff = staffrec

    print("You try and recruit more staff. You recruit " + str(staffrec) + " staff\n")
    if staff > 350:
        print("You cannot recruit any more staff!")

据我所知,您希望跟踪以前的选择,如果新选择与以前的选择相同,则应尝试再次选择,直到获得新值,以下是我的看法:

prev_staffrec = None
staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def choose_staffrec():
    global prev_staffrec # to make sure you're accessing it in the global scope
    staffrec2 = random.choice(staffrec)
    if staffrec2 == prev_staffrec:
        choose_staffrec() # run the function again if it's the same
    return staffRec2

这样就不需要一次又一次地创建staffrec 现在,无论你在哪里都有台词

staffrec = [1, 2, 3, 4, 5, 6, 7, 8, 9]
staffrec = random.choice(staffrec)

您可以替换为:

choose_staffrec()

相关问题 更多 >