繁体   English   中英

为什么我的代码不允许我循环某段代码,而是循环整个代码?

[英]Why does my code not allow me to loop a certain section of code, instead looping the entire code?

所以我目前正在使用 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 循环中,目前没有退出形式。 A quick fix is to move the line time = time - 1 to 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(" ")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM