虽然循环不起作用 - 找不到像我这样没有打破输入

时间:2016-07-16 05:08:55

标签: python

我正在python中创建这个游戏,如果用户投入的赌注超过他们的赌注或者他们的钱已经用完,我试图让程序停止运行。我过去曾经使用过while循环但是我不记得我是怎么做的以及其他循环问题;我已经尝试过他们的解决方案但没有优势。如果我可以得到这个循环,那么我想我可以得到我需要的第二个,这样用户可以继续玩而不必输入新的起始值感觉他/她将真正只玩1轮。无论如何,这是我的代码。

import time
import math
import random
continued='h'
continued2='g'
print("Welcome to gambling 50/50 shot game!!!"
      "You will put in an amount to start with and type in"
      "the amount you want to bet until your money you started with is gone."
      "You will choose either black or red. "
      "May the odds be in your favor.")
time.sleep(5)
while continued=='h':
    moneyleft=int(input("How much money are you going to start with? $:"))
    time.sleep(2)
    bet=int(input("How much money are you going to bet this round? $:"))
    if bet>moneyleft:
        print("sorry, you can't bet more then what you have!")
        time.sleep(2)
        print("Sorry, I do not play with cheaters!!!")
        continued=='z'
    time.sleep(2)
    colorchosen=int(input("Type 1, for black and 2 for red."))
    time.sleep(2)
    result=random.randint(1,2)
    if result==1:
        print("The color was black!!!")
    if result==2:
        print("The color was red!!!")
    time.sleep(3)
    if colorchosen==result:
        moneyleft=moneyleft+bet
        print("Congratulations!!! You won! Your money total is now $", moneyleft)
        time.sleep(3)
    if not colorchosen==result:
        moneyleft=moneyleft-bet
        print("Sorry, you lost. Your money total is now $", moneyleft)
        time.sleep(3)
        if moneyleft<=0:
            print("Sorry, your all out of money!!!")
            continued=='z'

2 个答案:

答案 0 :(得分:1)

修正了一些问题。值得注意的是,我做了评论者已经建议的内容:我在循环之外移动了初始money_left=输入,并使用break语句退出循环。

import time
import random

print("Welcome to gambling 50/50 shot game!!! "
      "You will put in an amount to start with and type in "
      "the amount you want to bet until your money you started with is gone. "
      "You will choose either black or red. "
      "May the odds be in your favor.")

time.sleep(5)
money_left = int(input("How much money are you going to start with? $"))

while True:
    time.sleep(2)
    bet = int(input("How much money are you going to bet this round? $"))

    if bet > money_left:
        print("Sorry, you can't bet more then what you have!")
        time.sleep(2)
        print("Sorry, I do not play with cheaters!!!")
        break

    time.sleep(2)

    color_chosen=int(input("Type 1 for black and 2 for red: "))
    time.sleep(2)

    result = random.randint(1,2)
    if result == 1:
        print("The color was black!!!")
    if result == 2:
        print("The color was red!!!")
    time.sleep(3)

    if color_chosen == result:
        money_left += bet
        print("Congratulations!!! You won! Your money total is now ${}.".format(money_left))
        time.sleep(3)
    else:
        money_left -= bet
        print("Sorry, you lost. Your money total is now ${}.".format(money_left))
        time.sleep(3)

        if money_left <= 0:
            print("Sorry, your all out of money!!!")
            break

答案 1 :(得分:0)

您永远不会改变变量的值。

当用户尝试下注的金额超过您执行的金额时:

if bet>money:
  ...
  continued=='z'

这只会评估为false,会将状态更改为&#34; z&#34;。看起来你打算写的是:

continued = 'z'

当用户用完资金时,您在代码中稍后再做同样的事情:

if moneyleft<=0:
  ...
  continued=='z'

此外,如果您更改&#34;继续&#34;的名称,它将使您的代码更加清晰。到&#34;继续&#34;你使用布尔而不是字符。它不清楚是什么&#39; g&#39;和&#39; z&#39;代表。如果继续是真或假,很明显你的意思是,你应该继续该程序,或者它是错误的,你不应该继续该程序。祝你的游戏好运:))