而循环不工作找不到类似的代码像我一样,是不是打破与inpu

2024-10-01 17:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python创建这个游戏,如果用户下注超过了他们的赌注或者他们的钱用完了,我试图让程序停止运行。我过去用过while循环,但我不记得我是怎么做的和其他while循环问题;我试过他们的解决办法,但没有成功。如果我可以得到这个循环,那么我想我可以得到第二个我需要的,这样用户就可以继续玩,而不必输入一个新的开始值感觉他/她将字面上只玩一轮。不管怎样,这是我的密码

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'

Tags: toimportyouiftimewithsleepresult
2条回答

您永远不会更改变量的值

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

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

这只会计算为false,将状态更改为continued to“z”。看来你想写的是:

continued = 'z'

当用户缺钱时,您在代码中再次执行了相同的操作:

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

此外,如果您将“continued”的名称改为“continued”,并且使用布尔值而不是字符,则代码会更清晰。不清楚“g”和“z”代表什么。如果continue是真是假,很明显你的意思是,你应该继续这个程序,如果continue是假,你不应该继续这个程序。祝你游戏顺利:)

解决了一些问题。值得注意的是,我做了评论员已经建议的事情:我将初始的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

相关问题 更多 >

    热门问题