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

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

Can i make variables work within If statements?

问题

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

  1. arrived = input('门铃响了吗?: ')
  2. if arrived == 'yes' or arrived == 'Yes':
  3. doorbell = input('是来自ifood吗?: ')
  4. elif arrived == 'no' or arrived == 'No':
  5. print('还没有,继续等待')
  6. else:
  7. print('请用是或否回答第一个问题')
  8. if doorbell == 'yes' or doorbell == 'Yes':
  9. print('您的ifood已经送到了!!!')
  10. elif doorbell == 'no' or doorbell == 'No':
  11. print('还不是您的ifood,继续等待')
  12. else:
  13. print('请用是或否回答问题')

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

英文:

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

  1. arrived = input('Have the doorbell ringed?: ')
  2. if arrived == 'yes' or 'Yes':
  3. doorbell = input('Is it from ifood?: ')
  4. elif arrived == 'no' or 'No':
  5. print('It is not here yet, keep waiting')
  6. else:
  7. print('Please answer the first question with yes or no')
  8. if doorbell == 'yes' or 'Yes':
  9. print('Your ifood have arrived!!!')
  10. elif doorbell == 'no' or 'No':
  11. print('It is not your ifood yet, keep waiting')
  12. else:
  13. 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 是错误的。更好的写法是:

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

或者

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

Your if is wrong. Better is:

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

or

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

答案2

得分: 0

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

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

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

完整解决方案:

  1. arrived = input('门铃响了吗?: ')
  2. if arrived == 'yes' or arrived == 'Yes':
  3. doorbell = input('是来自ifood吗?: ')
  4. if doorbell == 'yes' or doorbell == 'Yes':
  5. print('你的ifood已经送到了!')
  6. elif doorbell == 'no' or doorbell == 'No':
  7. print('这不是你的ifood,请继续等待。')
  8. else:
  9. print('请用是或否回答问题。')
  10. elif arrived == 'no' or arrived == 'No':
  11. print('还没有送到,请继续等待。')
  12. else:
  13. print('请用是或否回答第一个问题。')
英文:

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

  1. 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:

  1. arrived = input('Have the doorbell ringed?: ')
  2. if arrived == 'yes' or arrived == 'Yes':
  3. doorbell = input('Is it from ifood?: ')
  4. if doorbell == 'yes' or 'Yes':
  5. print('Your ifood have arrived!!!')
  6. elif doorbell == 'no' or 'No':
  7. print('It is not your ifood yet, keep waiting')
  8. else:
  9. print('Please answer the questions with yes or no')
  10. elif arrived == 'no' or arrived == 'No':
  11. print('It is not here yet, keep waiting')
  12. else:
  13. 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:

确定