在If语句中可以使用变量吗?

huangapple go评论169阅读模式
英文:

Can i make variables work within If statements?

问题

如何使这个变量在Python的if语句中工作?

arrived = input('门铃响了吗?: ')

if arrived == 'yes' or arrived == 'Yes':
    doorbell = input('是来自ifood吗?: ')
elif arrived == 'no' or arrived == 'No':
    print('还没有,继续等待')
else:
    print('请用是或否回答第一个问题')

if doorbell == 'yes' or doorbell == 'Yes':
    print('您的ifood已经送到了!!!')
elif doorbell == 'no' or doorbell == 'No':
    print('还不是您的ifood,继续等待')
else:
    print('请用是或否回答问题')

我是个初学者,这开始作为一个练习,我已经搜索了一段时间,我知道在Python中变量是全局的,但有没有办法使它工作?

英文:

How can i make this variable work withing the if statement in python?

arrived = input('Have the doorbell ringed?: ')

if arrived == 'yes' or 'Yes':
    doorbell = input('Is it from ifood?: ')
elif arrived == 'no' or 'No':
    print('It is not here yet, keep waiting')
else:
    print('Please answer the first question with yes or no')

if doorbell == 'yes' or 'Yes':
    print('Your ifood have arrived!!!')
elif doorbell == 'no' or 'No':
    print('It is not your ifood yet, keep waiting')
else:
    print('Please answer the questions with yes or no')

I'm a beginner and this started as an exercice, i've been searching this for a while and i know variables are global in python but is there a way to make it work?

答案1

得分: 0

你的 if 是错误的。更好的写法是:

  if arrived == 'yes' or arrived == 'Yes':

或者

  if arrived in ['yes', 'Yes']:
英文:

Your if is wrong. Better is:

  if arrived == 'yes' or arrived == 'Yes':

or

  if arrived in ['yes','Yes']:

答案2

得分: 0

首先,你的if语句有误 - 它需要包括第二个arrived ==,否则它不会起作用。它应该是这样的。

if arrived == 'yes' or arrived == 'Yes':

其次,你可能想将第二个if-elif-else语句放在第一个if语句内部,因为它只应在第一个if语句的条件满足时触发。

完整解决方案:

arrived = input('门铃响了吗?: ')

if arrived == 'yes' or arrived == 'Yes':
    doorbell = input('是来自ifood吗?: ')
    if doorbell == 'yes' or doorbell == 'Yes':
        print('你的ifood已经送到了!')
    elif doorbell == 'no' or doorbell == 'No':
        print('这不是你的ifood,请继续等待。')
    else:
        print('请用是或否回答问题。')
elif arrived == 'no' or arrived == 'No':
    print('还没有送到,请继续等待。')
else:
    print('请用是或否回答第一个问题。')
英文:

First, your if statement is wrong - it needs to include a second arrived == or it won't work. It should look like this.

if arrived == 'yes' or arrived == 'Yes':

Second, you probably want to chuck the second if-elif-else inside the first if statement, since it should only trigger if that if statement condition is fulfilled.

Full solution:

arrived = input('Have the doorbell ringed?: ')

if arrived == 'yes' or arrived == 'Yes':
    doorbell = input('Is it from ifood?: ')
    if doorbell == 'yes' or 'Yes':
        print('Your ifood have arrived!!!')
    elif doorbell == 'no' or 'No':
        print('It is not your ifood yet, keep waiting')
    else:
        print('Please answer the questions with yes or no')
elif arrived == 'no' or arrived == 'No':
    print('It is not here yet, keep waiting')
else:
    print('Please answer the first question with yes or no')

huangapple
  • 本文由 发表于 2023年5月28日 15:45:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76350453.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定