【问题标题】:I've set up a RNG and a continue button but it wont update the result after continuing我已经设置了一个 RNG 和一个继续按钮,但它不会在继续后更新结果
【发布时间】:2018-09-27 04:36:39
【问题描述】:
#Setting up RNG
loop = "y"
while loop == "y" or loop == "yes":
    from random import randint
    dice = (randint(1,10))
    dice2 = (randint(1,10))
    roll = (dice + dice2)
    win = 3
    loss = 2
    cash = 20
    if roll == 3 or roll == 7 or roll == 11 or roll == 17:
        cash += (win)
    else:
        cash -= (loss)
    #Starting game
    print("""Welcome to, Gambling for School!

    You have $20 and must earn as much money as possible

    If you roll a 3, 7, 11, or 17, you will win $3 but any other number 
takes $2

    You have a 20% of winning
""")
    x = input("Press ENTER to start.")
    #Results
    if roll == 11 or roll == 8 or roll == 18:
        print("You rolled an " + str(roll) + "!")
    else:
        print("You rolled a " + str(roll) + "!")
    print("")
    print("Cash - $" + str(cash))
    loop = input("Continue? (Y/N) ").lower()

必须更改缩进以将其显示为代码

当它运行时,我按 Enter 开始游戏,它正确地加减,但是当我选择继续时,它就像我从未输过或赢过任何钱一样。如果我的大脑死了,现在是凌晨 1 点,但我想不出任何办法来修复它

【问题讨论】:

    标签: python python-3.x random


    【解决方案1】:

    在每场比赛之前,您都用20 重新初始化变量cash。要修复游戏,只需将该代码移出循环即可。

    winloss 的初始化也可以移出循环,因为它们不会改变。

    from random import randint 语句同理,considered a good practice 将所有导入语句放在文件顶部。

    from random import randint
    
    #Setting up RNG
    loop = "y"
    win = 3
    loss = 2
    cash = 20
    while loop == "y" or loop == "yes":
        dice = (randint(1,10))
        dice2 = (randint(1,10))
        roll = (dice + dice2)
    
        if roll == 3 or roll == 7 or roll == 11 or roll == 17:
            cash += win
        else:
            cash -= loss
        #Starting game
        print("""Welcome to, Gambling for School!
    
    You have $20 and must earn as much money as possible
    
    If you roll a 3, 7, 11, or 17, you will win $3 but any other number 
    takes $2
    
    You have a 20% of winning
    """)
        x = input("Press ENTER to start.")
        #Results
        if roll == 11 or roll == 8 or roll == 18:
            print("You rolled an " + str(roll) + "!")
        else:
            print("You rolled a " + str(roll) + "!")
        print("")
        print("Cash - $" + str(cash))
        loop = input("Continue? (Y/N) ").lower()
    

    【讨论】:

      【解决方案2】:

      我会重新排序您的代码以使控制流更加清晰。

      为了比较 "if a in several elemets" 你应该使用 set()s - 如果有东西在里面查找时它们非常有效(并且对于其他集合操作)。

      对于打印查找str.format()python 3.6+ string interpolation: PEP-498

      您只能使用 2 个随机数的总和,您可以使用 random.choices(iterable, k=2) 一次性获得它们

      from random import choices
      
      cash = 20
      winAmount = 3
      lossAmount = 2
      
      #Starting game
      print("""Welcome to, Gambling for School!
      
          You have $20 and must earn as much money as possible
      
          If you roll a 3, 7, 11, or 17, you will win $3, else you loose $2
      
          You have a 20% chance of winning
      """)
      x = input("Press ENTER to start.")
      
      lucky_numbers = {3,7,11,17}
      
      # needed for outputting text
      pluralize = {8,11,18}
      win = False
      
      
      loop = "y"
      while loop and loop[0]== "y":
          sum_dice = sum(choices(range(1,11), k=2))
      
          if sum_dice in lucky_numbers:
              win = True
              cash += winAmount
          else:
              win = False
              cash -= lossAmount
      
          print("You {}. You rolled a{} {}!".format(
              "won" if win else "lost",
              "n" if sum_dice in pluralize else "", 
              sum_dice))
      
          print("")
          print("Cash - $" + str(cash))
          loop = input("Continue? (Y/N) ").lower().strip()
      

      输出:

      Welcome to, Gambling for School!
      
          You have $20 and must earn as much money as possible
      
          If you roll a 3, 7, 11, or 17, you will win $3, else you loose $2
      
          You have a 20% of winning
      
      Press ENTER to start.
      You lost. You rolled a 6!
      
      Cash - $18
      Continue? (Y/N) y
      You lost. You rolled a 16!
      
      Cash - $16
      Continue? (Y/N) y
      You lost. You rolled a 16!
      
      Cash - $14
      Continue? (Y/N) y
      You lost. You rolled a 15!
      
      Cash - $12
      Continue? (Y/N) y
      You won. You rolled a 7!
      
      Cash - $15
      Continue? (Y/N) n
      

      除了打印格式化输出使用三元(do x if this else y)操作符。更多内容:Does Python have a ternary conditional operator?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多