what is wrong with this code? why is my nested loop not executing inside while statement with the if statement This is a stock market game what is wrong with this code?
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)
*why is the if statement not working *
CodePudding user response:
move=input(": ") This is where you have done the mistake. When you get the input like this, the value is considered as type "String". (https://docs.python.org/3/library/functions.html#input)
Use, move = int(input(":")) as you have done in other places like getting the input for stock
CodePudding user response:
Move needs to be an integer, but it is a string when returned by the input function, so currently none of the conditions evaluate as true. You could make the if statement work by defining move as: move = int(input(": ")).
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}")
