我正在开发一个程序,根据给出的数字选择随机获胜者。我已经完成了这个程序,但是我找不到某个特定事物的解决方案。
这是我的代码:
player1Luck = 0
player1Luck = player1Luck / 2
player1Luck = player1Luck + 15
if player1Luck > 50:
player1Luck = player1Luck + 30
probabilities.extend([player1] * int(player1Luck))
print(player1Luck)
print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(player1Luck) + "%!")
else:
probabilities.extend([player1] * int(player1Luck))
print(player1 + " Your number is:", str(player1num) + "! You have a percentage to win of: ", str(player1Luck) + "%!")
所以,第一部分(在if语句之前)完全没问题,数字完全加起来等等。只有在if语句中,数字才会错误地加起来。例如,我说如果它高于50,则向player1Luck变量添加额外的30,而在我决定实现此功能之前,该数字远低于之前的数字。这是我在更改变量名之前看到的代码,以及尝试修复此问题。
newnum = player2num / 2
newnumadded = newnum + 10
if newnumadded > 50:
bonusnum = newnumadded + 50
print(player2 + " Your number is:", str(player2num) + "! You have a percentage to win of: ", str(bonusnum) + "%!")
probabilities.extend([player2] * int(bonusnum))
else:
print(player2 + " Your number is:", str(player2num) + "! You have a percentage to win of: ", str(newnumadded) + "%!")
probabilities.extend([player2] * int(newnumadded))
我知道问题可能存在于一些错误的代码中但是我已经仔细研究过并且非常挣扎,我对Python也很陌生,所以结构化代码可能对我自己来说还不够。