![](/img/trans.png)
[英]How to convert nested list strings to integers then sort them in python 3?
[英]How to sort multiple integers without making them array or list in Python
我剛剛學習編程,我已經開始學習 Python。 我有這個任務要做:模擬 4 名玩家的游戲。 每個玩家投擲一個骰子,直到投出 6 個,然后下一個玩家繼續投擲。 在大多數嘗試中擲出 6 的玩家為贏家。 程序應該寫出誰在投擲,他投了多少球。 如果 2 名玩家在相同的嘗試次數上投擲 6 次,則較早投擲的玩家獲勝。
任務來自 pythonladies BTW,目前我堅持按降序排序嘗試並選擇獲勝者。 我不想設置很多條件(如果,ifelse),而且我在第 5 章,我沒有學習數組/列表,所以我想在不使用它的情況下完成它。 到目前為止,這是我的代碼:
hrac 是播放器的意思
pokusy 意味着嘗試
from random import randrange, random
hrac1 = 0
pokusy1 = 0
hrac2 = 0
pokusy2 = 0
hrac3 = 0
pokusy3 = 0
hrac4 = 0
pokusy4 = 0
while hrac4 != 6:
while True:
if hrac1 != 6:
hrac1 = randrange(1, 7)
pokusy1 = pokusy1 + 1
print("Hráč 1 hodil: ", hrac1, " Toto bol jeho: ",pokusy1," pokus")
else:<br>
break
while True:<br>
if hrac2 != 6:
hrac2 = randrange(1, 7)
pokusy2 = pokusy2 + 1
print("Hráč 2 hodil: ", hrac2, " Toto bol jeho: ",pokusy2," pokus")
else:
break
while True:<br>
if hrac3 != 6:
hrac3 = randrange(1, 7)
pokusy3 = pokusy3 + 1
print("Hráč 3 hodil: ", hrac3, " Toto bol jeho: ",pokusy3," pokus")
else:
break
while True:
if hrac4 != 6:
hrac4 = randrange(1, 7)
pokusy4 = pokusy4 + 1
print("Hráč 4 hodil: ", hrac4, " Toto bol jeho: ",pokusy4," pokus")
else:
break
此外,如果您有簡化此代碼的想法,我會很高興:)
由於您不了解列表和 arrays 那么我可以告訴您以前我是 C++ 程序員時的解決方案:)
讓我們想象一下這些樣本數的嘗試次數:
pokusy1 = 1
pokusy2 = 2
pokusy3 = 3
pokusy4 = 4
temp = 0
那么你只需要以這種方式比較它們:
if(pokusy1 > pokusy2):
temp = pokusy1
else:
temp = pokusy2
if(pokusy3 > temp):
temp = pokusy3
if(pokusy4 > temp):
temp = pokusy4
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.