如何解决如何创建一个可以接受变量列表的if语句条件?
如何在if语句中创建一个条件,该条件将接受变量列表并打印出可以被5整除的数字?
firstNumber = input("Write the First Number: ")
secondNumber = input("Write the Second Number: ")
thirdNumber = input("Write the the Third Number: ")
fourthNumber = input("Write the the Fourth Number: ")
list1 = [firstNumber,secondNumber,thirdNumber,fourthNumber]
def divisibleByFive():
for x in list1:
if x%5 == 0:
print(x)
divisibleByFive()
解决方法
将其作为函数中的参数并在调用时传递:
You should ideally use a loop to achieve this but if you really want to do it using if-else use goto https://www.programiz.com/cpp-programming/goto
implementation using goto:
#include <iostream>
int main() {
int mood;
jump:
std::cout << "on a scale of 1-10 how are you? 1=worst 10=best ";
std::cin >> mood;
if ((mood<1)||(mood>10)){
std::cout << "InVaID eNtry (UR fEElinGS ArE StiLL valID)";
goto jump;
}else {
std::cout << "I am feeling like a(n) " << mood;
}
return 0;
}
Implementation using loop
#include <iostream>
int main() {
int mood;
do{
std::cout << "on a scale of 1-10 how are you? 1=worst 10=best ";
std::cin >> mood;
if ((mood<1)||(mood>10)){
std::cout << "InVaID eNtry (UR fEElinGS ArE StiLL valID)";
}else {
std::cout << "I am feeling like a(n) " << mood;
}
while((mood<1)||(mood>10))
return 0;
}
不喜欢路过?使用全局变量,但这是过大的:-(
firstNumber = input("Write the First Number: ")
secondNumber = input("Write the Second Number: ")
thirdNumber = input("Write the the Third Number: ")
fourthNumber = input("Write the the Fourth Number: ")
list1 = [firstNumber,secondNumber,thirdNumber,fourthNumber]
def divisibleByFive(l1):
for x in l1:
if int(x)%5 == 0:
print(int(x))
divisibleByFive(list1)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。