摘要:break:提前终止循环。continue:跳过当前迭代的循环内其余代码,并移至下一个迭代。else:当循环条件不再为 true 时执行一次代码块(在 for 和 while 循环中很有用。
for 循环用于迭代序列(例如列表、元组、字典、集合或字符串)。
fruits = ["apple", "banana", "cherry"]for fruit in fruits: print(fruit)只要给定条件为 true,while 循环就会重复执行代码块。
count = 0while countPython 还提供了控制语句来管理循环流:
break:提前终止循环。continue:跳过当前迭代的循环内其余代码,并移至下一个迭代。else:当循环条件不再为 true 时执行一次代码块(在 for 和 while 循环中很有用。休息:
for number in range(10): if number == 5: break print(number)#(start: SupportsIndex, stop: SupportsIndex, step: SupportsIndex = ..., /) -> rangefor num in range (1, 10, 3): print(num)continue:
for number in range(10): if number % 2 == 0: continue print(number)else 与 for 循环:
for number in range(5): print(number)else: print("Loop ended")student_heights = input("What is the student heights\n").split for n in range(0, len(student_heights)): student_heights[n] = int(student_heights[n])total_height = 0for height in student_heights: total_height += heightprint(f"total_height = {total_height}")number_of_student = 0for student in student_heights: number_of_student += 1print(f"number of student = {number_of_student}")average_height = round(total_height/number_of_student)print(f"average_height = {average_height}")>>What is the student heights120 130 152 258 58 56total_height = 774number of student = 6average_height = 129PS C:\Users\Hp\Desktop\Python>student_score = input("Enter the marks - ").splitfor n in range (0, len(student_score)): student_score[n] = int(student_score[n])highest_score = 0for score in student_score: if score > highest_score: highest_score = scoreprint(f"The highest score is {highest_score}")>>Enter the marks - 25 268 258 65 2041 6228 255 2 44 The highest score is 6228first = int(input("What is first value - "))second = int(input("What is second value - "))total = 0for num in range (first, second +1): total += numprint(f"The total is {total}.")>>What is first value - 1What is second value - 50The total is 1275.FizzBuzz 游戏是一个简单的编程挑战,通常用于教授基本的控制结构和逻辑。规则是:
打印从 1 到给定限制的数字。对于 3 的倍数,请打印 “Fizz” 而不是数字。对于 5 的倍数,请打印 “Buzz” 而不是数字。对于是 3 和 5 的倍数的数字,请打印 “FizzBuzz” 而不是数字。number = int(input("Enter number from 1 t0 100 - "))for num in range(1, number+1): if num % 3 ==0 and num % 5 == 0: print("fizzBizz") elif num % 3 == 0 : print("Fizz") elif num% 5 == 0 : print("Buzz") else: print(num) >>Enter number from 1 t0 100 - 1512Fizz4BuzzFizz78FizzBuzz11Fizz1314fizzBizzimport randomprint("-----Welcome to Password Generator!------")letters = int(input("How many letter you want?\n"))symbols = int(input("How many symbols you want?\n"))numbers = int(input ("How many number you want?\n")) password_list = # Generate letters #You can use "+=" or ".append"for _ in range(letters): password_list.append(random.choice('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')) # Generate symbolsfor _ in range(symbols): password_list += (random.choice('!@#$%^/?')) # Generate numbersfor _ in range(numbers): password_list += (random.choice('0123456789')) # Shuffle charactersrandom.shuffle(password_list) # Combine characters into passwordpassword = ''.join(password_list)print(f"Your password is: {password}")>>-----Welcome to Password Generator!------How many letter you want?10How many symbols you want?3How many number you want?5Your password is: VjVHE18SWJ35d?3@^G来源:自由坦荡的湖泊AI一点号
免责声明:本站系转载,并不代表本网赞同其观点和对其真实性负责。如涉及作品内容、版权和其它问题,请在30日内与本站联系,我们将在第一时间删除内容!