我必须以某种方式执行一个函数,当我执行它时,我将能够以如下所示的方式添加一个数字的所有除数。
这让我抓狂,我已经遇到同样的问题大约一个小时了。
def sum_divisors(n):
# Return the sum of all divisors of n, not including n
divisor = 1
while divisor < n:
if n%divisor==0:
return divisor
divisor = divisor + 1
else:
divisor = divisor + 1
print(sum_divisors(6)) # Should be 1+2+3=6
print(sum_divisors(12)) # Should be 1+2+3+4+6=16
Run Code Online (Sandbox Code Playgroud) 我刚刚开始学习 shell,对 read line(variable) 命令如何读取文件感到困惑
done < filename.
Run Code Online (Sandbox Code Playgroud)
代码 :
while read LINE; do
echo "This is a downvote"
done < inputfile
Run Code Online (Sandbox Code Playgroud)
我的假设是,当 while 运行行变量为空时,它应该为 false,而 while 循环退出,但事实并非如此。是否读取循环中的文件检查,并且我知道输入重定向命令已传递到文件。有人可以解释一下它是如何工作的吗?
我有一个名为“事件”的自定义帖子类型。所有这些事件页面都有一个ACF 日期字段来输入事件开始日期。在事件存档页面中,我想将这些事件分为两组。
所以即将推出的活动将是这样的:
<?php
$today = date('Ymd');
$args = array(
'post_type' => 'events',
'nopaging' => true,
'orderby' => 'meta_value_num',
'meta_key' => 'event_start_date', //ACF date field
);
$upcoming_events = new WP_Query( $args );
if ( $upcoming_events->have_posts() ) :
?>
<h2>Upcoming Events</h2>
<ul>
<?php while ( $upcoming_events->have_posts() ) : $upcoming_events->the_post(); ?>
<li>
Title: <?php the_title(); ?><br>
Date: <?php echo get_field('event_start_date'); ?>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
我知道有 meta_query …
list1 = ["apple", "pear", "", "strawberry", "orange", "grapes", "", "watermelon"]
list2 = []
x = 0
while x < len(list1):
if len(list1[x]) > 0:
list2.append(list1[x])
print(list2)
Run Code Online (Sandbox Code Playgroud)
我尝试运行这段代码,但似乎不起作用。我应该如何不使用列表理解或其他方法来修改此代码?
while (<>) { $file .= $_};该代码由于某种原因挂起,或者在查询时不再继续执行。这是为什么?
一旦我用输入的文本启动代码,就不会发生比它输出的更多的事情task1,然后它就会挂起。
代码:
#!/usr/bin/perl -w
use strict;
use JSON;
my $json = JSON->new->allow_nonref;
my $file = "";
print('task1');
while (<>) { $file .= $_ };
print('task2');
my $json_output = $json->decode( $file );
my ($c, $i, $cstr, $istr);
foreach my $cert (@$json_output) {
print('task3');
$i = $json_output->{i};
$c = $json_output->{c};
$istr = join("", map { sprintf("%02x",$_) } @$i);
$cstr = pack("C*", @$c);
open(F, ">$istr.der"); print F $cstr; close(F);
print('done.');
}
Run Code Online (Sandbox Code Playgroud)
输出:
task1
Run Code Online (Sandbox Code Playgroud) 考虑证明以下 while 循环的正确性,即我想表明,给定循环条件开始时,它最终将终止并导致最终断言为真。
int x = 0;
while(x>=0 && x<10){
x = x + 1;
}
assert x==10;
Run Code Online (Sandbox Code Playgroud)
在不使用循环展开的情况下,用于检查正确性的 SMT-LIB 的正确翻译是什么?
所以我昨天刚开始 C++,我有相当多的 java 经验,所以这就是原因,我尝试运行这段代码,由于某种原因 while 循环没有循环,我尝试将 if break 语句从 更改ask==false为ask=false,这最终会导致无限循环,甚至不接受用户输入。
这是代码:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
double raduis;
const double pi = 3.14;
bool ask;
while(true){
cout << "Enter the raduis of the circle:"<< endl;
cin >> raduis;
double circ = 2*pi*raduis;
double area = pi*pow(raduis,2);
cout << "The Circumference of the circle is: "<< circ <<endl;
cout << "The Area of the circle is: "<< area<<endl;
cout <<"Would you like to …Run Code Online (Sandbox Code Playgroud) 问候大家,
我有这个表(没有 Res_Problem):
| ID | 问题 | X | 影响 | 概率 | 解决问题 |
|---|---|---|---|---|---|
| 识别号1 | 12 | 数据中心1 | 1 | 2 | (12-2)=10 |
| 识别号1 | 12 | 数据中心2 | 2 | 2 | (10-4)=6 停止 |
| 识别号1 | 12 | 数据中心3 | 1 | 0 | 无循环 |
| 识别号1 | 12 | 数据中心4 | 1 | 0 | 无循环 |
| 识别号2 | 10 | IDB1 | 1 | 2 | 新循环 (10-2)=8 |
| 识别号2 | 10 | IDB1 | 1 | 2 | (8-2) = 6 停止 |
我想做一个循环,将影响和概率相乘,直到获得所需值(例如 6),然后停止循环,直到达到 6。但再次开始 ID2 上的循环...等等,任何建议?
我认为它必须是这样的:
while (df['问题'] - df['影响']*df['影响'] < 6): df['循环'] = res
循环应创建“Res_Problem”列
AHashMap<Integer, Integer>有 10 个条目,但我只想打印 3 个条目。
代码:
HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
hm.put(2, 1);
hm.put(5, 3);
hm.put(7, 2);
hm.put(4, 1);
hm.put(6, 3);
hm.put(8, 2);
hm.put(9, 1);
hm.put(3, 3);
hm.put(1, 2);
hm.put(0, 2);
Iterator <Map.Entry<Integer, Integer>> itr = hm.entrySet().iterator();
int n=4;
int i=0;
while(itr.hasNext()){
if(i<n){
Map.Entry<Integer, Integer> entry = itr.next();
System.out.println(entry.getKey()+" repeated "+entry.getValue());
}
i++;
}
Run Code Online (Sandbox Code Playgroud)
输出
0 repeated 2
1 repeated 2
2 repeated 1
3 repeated 3 //program will wait for 2 or 3 seconds here
4 …Run Code Online (Sandbox Code Playgroud) 我有一个WHILE循环,检查特定学生的标记.但是,如果值无效(输入小于0且大于100),则不会循环:
int marks= -1;
System.out.print("Student Marks (/100): ");
while (((marks< 0) || (marks> 100))) {
try {
marks = Integer.parseInt(sc.nextLine());
break;
} catch (NumberFormatException nfe) {
System.err.println("Error: Invalid Mark(s)");
System.out.print("Student Marks (/100): ");
}
}
Run Code Online (Sandbox Code Playgroud)
如果输入了数字以外的字符,它会捕获异常.
但如果值小于0或大于100,它不会再次循环.
我试过对它做了很多改动,但没有结果.
任何帮助表示赞赏!