完成此python任务的提示
我对编码和python非常陌生,希望能得到一些帮助来完成我收到的这个小任务 本项目的要求如下:完成此python任务的提示,python,loops,Python,Loops,我对编码和python非常陌生,希望能得到一些帮助来完成我收到的这个小任务 本项目的要求如下: 创建一个在1到100之间循环的Python应用程序 这些数字将与它们的平方值一起打印出来 当平方值达到或超过200时,应用程序应停止 重新配置应用程序以获取用户值,以生成最大为的平方值 目前,我可以创建前两点,并对允许用户输入应用程序有一些想法。下面是代码 # The program to find the square of all numbers present in the list # v
- 创建一个在1到100之间循环的Python应用程序
- 这些数字将与它们的平方值一起打印出来
- 当平方值达到或超过200时,应用程序应停止
- 重新配置应用程序以获取用户值,以生成最大为的平方值
# The program to find the square of all numbers present in the list
# variable to store the squared numbers
square = 1
x = 200
# List of numbers
numbers = range(1,100)
#iterate over the list
for val in numbers:
square = val*val
# prints the output
print ("The square of ", val, "is", square)
您需要提供正方形的上限。我假设用户输入将是正方形的上限。我会这样编码:
n = int(input())
i = 1
while (i*i<n):
print(i, i*i)
i+=1
n=int(输入())
i=1
虽然(i*i因为您的问题说当平方值达到或超过200时程序停止,我将提供我的实现:
#square = 1 You can comment out this line since this is redundant.
#x = 200 Even this line is redundant.
# List of numbers
numbers = range(1,100)
#iterate over the list
for val in numbers:
square = val*val
if square >= 200:
exit() #I prefer exit() statement over break statement, you can decide what would suit better for you
print ("The square of ", val, "is", square)
你有什么特别的问题吗?看起来你已经做得很好了。循环可以用break
语句中断。我说得很糟糕。它确实有效,但有一些方法可以改进。例如,你使用了一个从未更改过的标志变量。想想你可以如何改进它。检查我上面的评论是的,我注意到了flag变量,但我已经输入了程序。但是如果你这么说,我会编辑。。。