簡體   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