如何解决“声明似乎无效”错误?

时间:2018-11-26 04:47:28

标签: python python-3.x

任务是让用户输入一些值,如果花了额外的现金来代替基本商品,但是如果没有花额外的现金,那么我希望程序继续进行到下一行代码。

>

我的代码哪里出错了?

print("Did you spend extra to replace a capital goods\n")

variableExpense = 'Yes'
variableExpense = 'No'

if variableExpense == 'Yes':
    variableExpense = input('How much did you spent: \n')
else:
    if variableExpense == 'No':
    print('print some statement.\n')

1 个答案:

答案 0 :(得分:0)

您当前正在将variableExpense分配给值Yes,然后在下一行将其设置为No,因此以下语句if variableExpense == 'Yes'将永远不会等于true 。像这样尝试

print("Did you spend extra to replace a capital goods\n")

variableExpense = input("Yes or No: ")

if variableExpense.lower() == 'yes':
    extraSpent = input('How much did you spent: \n')
else:
    if variableExpense.lower() == 'no':
        print('print some statement.\n')