<p>正如<a href="https://stackoverflow.com/users/3890632/khelwood">khelwood</a>在评论中指出的,在这种情况下,实际循环比重复循环要好。如果在<code>main</code>内部调用<code>main</code>,则在不需要的情况下,会越来越深入地进行嵌套函数调用。通过<code>while</code>循环,您可以继续执行某些操作,直到某个条件不再为真。一个函数既可以接受参数,也可以返回一个值,这样每次游戏都可以从不同的平衡点开始</p>
<p>我做了一些其他的调整。我用链式<code><</code>比较重写了你的赢/输支票,并将它们分成两行以便于阅读。在括号内,可以添加换行符,而不会影响代码执行。Python也会自动组合以这种方式分解的字符串,这就是我在介绍文本中所做的。我还修改了一些变量打印以使用<a href="https://realpython.com/python-f-strings/" rel="nofollow noreferrer">f-strings</a></p>
<pre class="lang-py prettyprint-override"><code>import random
def main():
print("Welcome to Python Acey Ducey Card Game")
print()
print("Acey-ducey is played in the following manner: the dealer "
"(computer) deals two cards faced up and you have an option "
"to bet or not bet depending on whether or not you feel the "
"card will have a value between the first two. If you do not "
"want to bet, enter a $0 bet.")
bankbalance = 100
playagain = "yes"
while playagain == "yes":
bankbalance = play(bankbalance)
playagain = input("Would you like to play again yes/no: ")
print(f"You leave with ${bankbalance}")
def play(bankbalance):
print()
print(f"You currently have ${bankbalance}")
print("These cards are open on the table:")
print()
print("First card:")
firstcard = random.randint(1,13)
print(firstcard)
print("Second card:")
secondcard = random.randint(1,13)
print(secondcard)
playerinput = input("Enter your bet: ")
playerinput = int(playerinput)
dealercard = random.randint(1,13)
print(f"The card you drew was {dealercard}!")
if (firstcard < dealercard < secondcard or
secondcard < dealercard < firstcard
):
print("You win!")
return bankbalance + playerinput
else:
print("You lost!")
return bankbalance - playerinput
if __name__ == '__main__':
main()
</code></pre>
<p>另外,请注意,您的代码目前允许您下注超过您的赌注,并且在您为负数时继续下注。如果这不是故意的,想想你可以如何避免</p>