3-30 循环

while

浮点数不用作判定条件

在这里插入图片描述

for

range: an object that produces a sequence of integers from start (inclusive) to stop (exclusive) by step.

	for i in range(initvalue,endvalue):
		print(i)
	for var in sequence:
		print(val)
	# in function :range(a) 等于 rang(0,a)
	# in function :range(a,b,k) 以步长k从a走到b	

最大公约数(GCD)

  1. 常规的方法,i是不是n1的约数,是不是n2的约数,依次遍历到n1和n2 的较小的值
import math
x1, x2 = eval(input("enter x1,x2:"))
gcd = 0
if x1 > x2:
    x1, x2 = x2, x1
for i in range(1, x1+1):
    if x1 % i ==0 & x2 % i == 0:
        gcd = i

print(gcd, end="\t")
print(math.gcd(x1, x2))

  1. 辗转相除法:
	import math
x1, x2 = eval(input("enter x1,x2:"))
gcd = 0
if x1 > x2:
    x1, x2 = x2, x1

# 大的除以小的,再将余数进行除
div = 0
# x2 is bigger than x1
while x1 != 0:
    temp = x2 % x1
    x2 = x1
    x1 = temp

print(x2)
print(math.gcd(x2, x1))

预测未来学费

	import math

tuition = eval(input("please enter tuition:"))
target = 2 * tuition
i=0
while tuition < target:
    tuition *= 1.07
    i+=1

print("total tuition is ", format(tuition, ".2f"), "passed ", format(i, ".2f"), "years")

蒙特卡罗模拟

	import random

NUMBER_OF_TRIALS = 1000000

numberOfHits = 0

for i in range(NUMBER_OF_TRIALS):
    x = random.random()*2 - 1
    y = random.random()*2 - 1

    if x**2 + y**2 <= 1:
        numberOfHits += 1

print("pi = ", 4 * numberOfHits / NUMBER_OF_TRIALS)

break continue

  1. and和not 和c不同
  2. break 跳出整个循环,continue 语句只是退出当前循环
在Uniapp中,可以使用v-for指令来进行循环渲染。根据提供的引用内容,可以看到一个示例代码,其中使用了v-for来循环展示一个数组的内容。具体的代码如下所示: ```html <template> <view class="content"> <view>@@@@@ 水果价目表 @@@@@</view> <view v-for="(item,index) in list" :key="item.id"> 名称:{{item.text}} || 价格:{{item.price}}元/公斤 </view> </view> </template> <script> export default { data() { return { list: \[ { id: 0, text: "香蕉🍌", price: 30 }, { id: 1, text: "葡萄🍇", price: 40 }, { id: 2, text: "橙子🍊", price: 20 } \] }; } } </script> <style> </style> ``` 在这个示例中,v-for指令被应用在一个view标签上,通过遍历list数组的每个元素,将其渲染为一个view标签。在循环过程中,可以通过item和index来访问每个元素的属性和索引。通过:key属性,可以为每个循环项指定一个唯一的标识符。 这样,当页面渲染时,会根据list数组的内容动态生成相应的view标签,展示出水果的名称和价格。 #### 引用[.reference_title] - *1* *2* *3* [VUE(uni-app框架)开发微信小程序②-for循环](https://blog.csdn.net/qq_39075676/article/details/127371174)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值