亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

作用于python中的變量變化

作用于python中的變量變化

慕后森 2023-06-13 16:21:37
所以我正在嘗試制作一個醫院模擬(基于文本)并且可以選擇招募更多員工。我已經想出如何限制他們可以招募的次數(限制 350 名員工)并且他們獲得的數量應該是隨機的(random.choice(staffrec),但是一旦從 staffrec 生成隨機數,它就會保持不變相同,我希望代碼再次運行并產生不同的結果。這是我的代碼。(抱歉,我已經把所有的都放了,所以我不會錯過任何東西)import timeimport randomstaff = 100wards = 20beds = 140patients = 80staffrec = [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 + staffrecwhile option != Fopt:? ? option = input("""What would you like to do:A: View number of patientsB: View number of bedsC: View number of wardsD: View number of staffE: Recruit more staffF: 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 任何幫助將不勝感激!
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

staffrec您在主要游戲邏輯之前分配值并且從不更新新的隨機值,因此每次您進入添加新兵的循環塊時它的值都是相同的。如果您希望它每次都更改,只需random在需要隨機數時調用 only 即可。

我還建議使用randintover choice 方法,因為它會在任意兩個值之間生成一個隨機數。例如,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!")


查看完整回答
反對 回復 2023-06-13
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

據我了解,您想跟蹤之前的選擇,如果新選擇與之前的選擇相同,則應嘗試再次選擇,直到獲得新值,這是我的看法:


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()


查看完整回答
反對 回復 2023-06-13
?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

關于您的代碼,有幾件事對我來說很突出:

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

這兩行沒有任何作用,因為staffrec無論如何您都要替換 later 的值。此外,根據經驗,避免重復使用變量名,特別是如果您將它們重新分配給完全不同的類型(以前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來生成一個新值,就像我之前提到的那樣。


查看完整回答
反對 回復 2023-06-13
  • 3 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號