对Python不熟悉,但通过努力学习并尝试进行良好的实践。希望有人能为我的python程序指明正确的方向:powerball。它在计算机和用户之间,每个人在1-9之间选择3个唯一的(非重复的)数字,在1-3之间选择一个powerball数字。我想添加一个money变量,该变量将在main()函数之间共享(它告诉您“欢迎”和“您当前的钱是:(money)),也在compare\u winnings()函数之间共享,它比较我的两个列表,并将总数添加到(money)变量中
我通过google等做了一些研究,发现把money变量放在每个函数之外会把它变成一个“全局变量”,这似乎很有用,因为它将在两个函数中使用。我还了解到,使用语法“global”是不好的做法。但是,代码运行良好,在compare\u winnings()函数中,钱会被更新。当游戏提示您是否要再次玩(另一个功能)时,它会重新开始,并且钱会回到原来的值(从20开始)
def main():
print("Each ticket costs $1. Your winnings will be totaled. Current money is",money, "dollars." )
start = input("\n Are you ready? [y/n]: ")
if start == "y" or start == "yes":
#erased variables for legibility; gathers numbers from computer and user
compare_winnings(actual, chosen,cpb, comp_cpb,money)
else:
play_again()
def compare_winnings(actual, chosen, cpb, comp_cpb,money):
counter = 0
print("You chose: ", chosen) #user-inputted
print("Ticket was actually: ", actual) #random (actual)
same_values = set(actual) & set(chosen)
#print ("These are the values that are the same",same_values)
if len(same_values) > 0:
counter += len(same_values)
print("Numbers correct was : ",counter)
if counter == 1 and cpb != comp_cpb :
print("You won $1")
money += 1
print("total money is:", money)
play_again()
def play_again():
play = input("Do you want to play again? ")
if play == "y" or play == "yes":
main()
else:
print("Program will exit. Thanks for playing.")
我希望money变量会被更新(并保留),直到他们决定停止玩游戏。但是,当他们决定再次玩或在main()时,它似乎会重新启动
目前没有回答
相关问题 更多 >
编程相关推荐