为什么我的代码不允许循环某一部分代码,而是循环整个代码



所以我目前正在使用python为我的任务创建一个小型股票经纪人游戏,我遇到了一个问题。我的下一节课要到下星期二,我似乎找不到任何好的解决方案。基本上,这个代码最初是为了在玩家决定等待一天时将时间减少1,但无论玩家等待多少次,时间都保持在20。我也不确定如何让我的代码循环用户输入部分:"你想买股票、卖股票还是等一天?"直到用户说他想等。这个问题有解决办法吗?如果有,解决办法是什么?这是我所说的代码:

stocks = 0
time = 21
money = 100
print("You have $" + str(money) + " in your bank.")  
while time > 0:
    stock = random.randint(1,20)
    time = time - 1
    while True:        
        print("You have " + str(time) + " days remaining until the market closes")
        print("Stocks are currently worth: $" + str(stock)) 
        choice = input("Did you want to buy stocks, sell stocks or wait a day?  ")
        if choice.casefold() == "buy":       
            while True:
                numberBuy = int(input("Enter the number of stocks that you would like to buy:  "))
                if numberBuy * stock > money:
                    print("You don't have the money for that")
                else:
                    break
            money = money - (numberBuy * stock)
            stocks = stocks + numberBuy
            print("You bought " + str(numberBuy) + " stocks and you spent $" + str(numberBuy * stock) + " buying them. You now have $" + str(money) + " and you have " + str(stocks) + " stocks")
        elif choice.casefold() == "sell":
            while True:
                numberSell = int(input("Enter the number of stocks that you would like to sell:  "))
                if numberSell > stocks:
                    print("You don't have enough stocks for that. You have " + str(stocks) + " stocks and you want to sell " + str(numberSell))
                else:
                    break
            stocks = stocks - numberSell
            money = money + (numberSell * stock)
            print("You sold " + str(numberSell) + " stocks and you made $" + str(numberSell * stock) + " selling them. You now have $" + str(money) + " and you have " + str(stocks) + " stocks")
        elif choice.casefold() == "wait" or "wait a day" or "wait day":
            print("You wait for the next day to come. At the end of the day, you have $" + str(money) + " in your bank account and you have " + str(stocks) + " stocks to")
            print("your name")
            print(" ")
            print("===========================================================================================================")
            print(" ")```  

time从未更改的原因是,所有基于用户输入选择的代码都在第二个while循环中,而该循环目前没有退出形式。快速解决方法是将行time = time - 1移动到if语句中的相应选项。此外,我认为你应该完全去掉第二个while循环,因为股价永远不会改变。

while time > 0:
    stock = random.randint(1,20)
    print("You have " + str(time) + " days remaining until the market closes")
    print("Stocks are currently worth: $" + str(stock)) 
    choice = input("Did you want to buy stocks, sell stocks or wait a day?  ")
    if choice.casefold() == "buy":       
        while True:
            numberBuy = int(input("Enter the number of stocks that you would like to buy:  "))
            if numberBuy * stock > money:
                print("You don't have the money for that")
            else:
                break
        money = money - (numberBuy * stock)
        stocks = stocks + numberBuy
        print("You bought " + str(numberBuy) + " stocks and you spent $" + str(numberBuy * stock) + " buying them. You now have $" + str(money) + " and you have " + str(stocks) + " stocks")
    elif choice.casefold() == "sell":
        while True:
            numberSell = int(input("Enter the number of stocks that you would like to sell:  "))
            if numberSell > stocks:
                print("You don't have enough stocks for that. You have " + str(stocks) + " stocks and you want to sell " + str(numberSell))
            else:
                break
        stocks = stocks - numberSell
        money = money + (numberSell * stock)
        print("You sold " + str(numberSell) + " stocks and you made $" + str(numberSell * stock) + " selling them. You now have $" + str(money) + " and you have " + str(stocks) + " stocks")
    elif choice.casefold() == "wait" or "wait a day" or "wait day":
        time = time - 1
        print("You wait for the next day to come. At the end of the day, you have $" + str(money) + " in your bank account and you have " + str(stocks) + " stocks to")
        print("your name")
        print(" ")
        print("===========================================================================================================")
        print(" ")

最新更新