首页 > 解决方案 > 为什么我的代码不允许我循环某段代码,而是循环整个代码?

问题描述

所以我目前正在使用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(" ")```  


标签: pythonloops

解决方案


永远不会改变的原因time是因为所有基于用户输入选择的代码都在第二个 while 循环中,目前没有退出形式。A quick fix is to move the line time = time - 1to the appropriate option in the if statement. 另外我认为您应该完全删除第二个 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(" ")

推荐阅读