我已经编写了这段代码,但是当我编写64时,它说不是完美的立方体,为什么?
num=int(input('num: '))
for i in range(0,num+1):
if num==i**3:
print('It is perfect cube')
else:
print('not perfect cube')
break
答案 0 :(得分:1)
如果您检查的num == i**3
的每个值的i
为假,则只想打印“不是完美的立方体”,而不仅仅是其中之一。基本上,您希望将else
附加到for
循环,而不是if
语句,
for i in range(num+1):
if num == i**3:
print('It is a perfect cube')
break # Don't need to check more
else:
# Only executed if the loop ends "naturally", not via a break
print('It is not a perfect cube')
如果您想一次i ** 3 > num
停止循环,这会变得更加复杂。您需要一个标志来表明您是由于发现多维数据集根而中断,还是因为您知道没有中断。一种可能的方法:
cube = False
for i in range(num+1):
tmp = i ** 3
if num == tmp:
print('It is a perfect cube')
cube = True
break
elif num < tmp:
break
else:
if not cube:
print('Not a cube')
答案 1 :(得分:0)
$ssl=new ssl();
$x="this was encrpyted";
echo "<br />1".$x;
$json=$ssl->encrypt($x);
echo "<br />2".$json;
echo "<br />3".$ssl->decrypt($json);
class ssl {
private $cipher = "aes-128-gcm";
private $options=0;
public function encrypt($plaintext) {
$key=openssl_random_pseudo_bytes(16);
$ivlen=openssl_cipher_iv_length($this->cipher);
$iv=openssl_random_pseudo_bytes($ivlen);
$ciphertext=openssl_encrypt(
$plaintext,
$this->cipher,
$key,
$this->options,
$iv,
$tag
);
$a=[];
$a["key"]=bin2hex($key);
$a["iv"]=bin2hex($iv);
$a["ciphertext"]=$ciphertext;
return json_encode($a);
}
public function decrypt($json) {
$a=json_decode($json,true);
return openssl_decrypt(
$a["ciphertext"],
$this->cipher,
hex2bin($a["key"]),
$this->options,
hex2bin($a["iv"])
);
}
}
答案 2 :(得分:0)
一种快速版本,找到解决方案后将停止评估值。
num = int(input('num: '))
if any(num == i ** 3 for i in range(num)):
print('perfect cube')
else:
print('not perfect cube')