【发布时间】:2022-01-15 05:25:56
【问题描述】:
这段代码有什么问题? 为什么我的嵌套循环没有在带有 if 语句的 while 语句中执行 这是一个股市游戏 这段代码有什么问题?
import random
amount = 0
cash = 1000
y = input("How many days would you like to play ? : ")
x = 0
move=0
y=int(y)
while x < y:
price = int(random.randint(50 , 1000))
print("Stock Price:" )
print (price )
print("Cash: ")
print(cash)
print("Stocks Owned: ")
print(amount)
print("Days Remaining: ")
print(x)
print("What do you want to do? 1 to buy stocks, 2 to sell stocks, 3 to skip a day.")
move=input(": ")
#taking the input from user
if move == 1:
stock=int(input("how many stocks would you like to buy? : "))
amount = (price * stock)
cash = cash - (price * stock)
x = +1
print("--------------------------------------------------------------------------------------------------")
elif move == 2:
cash = cash + (amount * price)
amount = 0
x = x + 1
print("--------------------------------------------------------------------------------------------------")
elif move == 3:
x = x + 1
print("--------------------------------------------------------------------------------------------------")
print("You made " + cash + "$ in " + " days.")
print("Score: " + cash / y)
*为什么if语句不起作用*
【问题讨论】:
-
我没有看到任何嵌套循环,但在
move == 1下,你有x = +1而不是x = x + 1。每次购买股票时,所花费的天数都会重置。
标签: python-3.x if-statement random module integer