python - 为什么我的代码不允许我循环某段代码,而是循环整个代码?
问题描述
所以我目前正在使用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(" ")
推荐阅读
- .net-core - .Net Core 3.1 是否具有 InheritedExportAttribute?
- javascript - 使用名称获取输入的 id
- c++ - C/C++ DirectX9 将矩形中的像素复制到屏幕上的另一个矩形
- java - 晚上好,为什么我的代码没有在控制台中打印任何内容,尽管他有一个“system.out.println”指令?
- ios - Websockets 在 iOS 和 Safari 上不起作用 - OSSStatus 错误 9837
- python - 获取具有行单元条件的最大值列
- python - 带有交互图的循环函数
- javascript - 降低画布内动画粒子的 CPU 消耗
- ruby - 从每日 Daru 时间序列中提取每周或每月数据
- css - 如何在 CSS 网格中悬停时更改颜色