为什么我的嵌套循环不在while语句和if语句中执行



这段代码有什么问题为什么我的嵌套循环不在while语句和if语句内部执行这是一场股票市场游戏这个代码出了什么问题?

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 = input(": ")

这就是你犯错误的地方。当你得到这样的输入时,该值被认为是类型";字符串";。(https://docs.python.org/3/library/functions.html#input)

使用,

move = int(input(":"))

就像你在其他地方所做的那样,比如获得股票的输入

Move需要是一个整数,但当输入函数返回时它是一个字符串,因此目前没有任何条件计算为true。通过将move定义为:move = int(input(": ")),可以使if语句起作用。

import random
amount = 0
cash = 1000
y = int(input("How many days would you like to play ? : "))
x = 0
move = 0
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.")
#taking the input from user
move = int(input(": "))
if move == 1:
stock = int(input("how many stocks would you like to buy? : "))
amount = (price * stock)
cash = cash - (price * stock)
x = x + 1
print("--------------------------------------------------------------------------------------------------")
elif move == 2:
cash = cash + (amount * price)
amount = 0
x = x + 1
print("--------------------------------------------------------------------------------------------------")
elif move == 3:
x = x + 1
print("--------------------------------------------------------------------------------------------------")
print(f"You made ${cash} in {y} days.")
print(f"Score: {cash / y}")

相关内容

  • 没有找到相关文章

最新更新