【问题标题】:why is my nested loop not executing inside while statement with the if statement为什么我的嵌套循环没有在带有 if 语句的 while 语句中执行
【发布时间】: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


【解决方案1】:
move = input(": ")

这是你犯错的地方。当您获得这样的输入时,该值被视为“字符串”类型。 (https://docs.python.org/3/library/functions.html#input)

使用,

move = int(input(":"))

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

【讨论】:

    【解决方案2】:

    Move 需要是一个整数,但是当输入函数返回时它是一个字符串,所以目前没有一个条件评估为真。您可以通过将 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}")
    

    【讨论】:

      猜你喜欢
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 1970-01-01
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多