【发布时间】:2018-07-30 14:31:07
【问题描述】:
首先这是我应该完成的任务 "第二个编码挑战将使用 while 循环来解决这两个条件
给定任何称为极限的数字,我们将找到并打印所有等于或低于极限的正方形,因此:
给定 30,您的第一个解决方案将打印 1、4、9、16、25
第二个解决方案将打印多维数据集,因此:
给定 30,您的第二个解决方案将打印 1、8、2"
reply = True
while reply == True:
limit = input("Choose any number ")
if limit.isnumeric():
limit = float(limit)
reply = False
else:
print("What you have entered is not a number")
reply = True
number = 1
square = list()
cube = list()
while number > 0:
squarenumber = number ** 2
if squarenumber > limit:
break
square.append(squarenumber)
cubenumber = number ** 3
if cubenumber > limit:
break
cube.append(cubenumber)
number = number+1
print("All the squares below or equal to",limit,"are",*square)
print("All the cubes below or equal to", limit,"are",*cube)
这是我编写的代码,当我将 30 作为“限制”的值时,它显示
All the squares below or equal to 30.0 are 1 4 9 16
All the cubes below or equal to 30.0 are 1 8 27
所以我不太确定我做错了什么
【问题讨论】:
-
while reply == True:- 如果表达式的计算结果为布尔值,则不应在while语句中比较True或False,只需写while reply: -
确实,这将算作一种良好的编程习惯。以前的代码看起来更容易理解(从新手的角度来看)
标签: python python-3.x cubes