If语句评估?

2024-10-02 02:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前对这行代码有问题

roomChoice = input("Choose 1-3: ")

if roomChoice == 1:
    print("You went to the bar, and lost $5, your total loot is : " + main.totalLoot)
else:
    print(roomChoice)

每当我输入1时,它就会输出1,而不是计算第一个if语句


Tags: andtheto代码youinputyourif
2条回答

使用“1”而不是1:

roomChoice = input("Choose 1-3: ")

if roomChoice == "1":
    print("You went to the bar, and lost $5, your total loot is : " + main.totalLoot)
else:
    print(roomChoice)

尝试在if语句之前将输入值强制转换为整数。 使用

if int(roomChoice) == 1:
    print("You went to the bar, and lost $5, your total loot is : " + main.totalLoot)
else:
    print(roomChoice)

相关问题 更多 >

    热门问题