for number inrange(10, 20): for factor1 inrange(2, number): if number % factor1 == 0: factor2 = number / factor1 print(f'{factor1}*{factor2:0.0f}={number}') break else: print(f'{number} is a prime number')
for-if-else
1 2 3 4 5 6 7 8 9
for number inrange(10, 20): for factor1 inrange(2, number): if number % factor1 != 0: print(f'{number} is a prime number') break else: factor2 = number / factor1 print(f'{factor1}*{factor2:0.0f}={number}') break
while-else
1 2 3 4 5 6 7 8 9 10 11 12
number = 10 while number < 20: factor1=2 while factor1<20: if number%factor1==0: factor2=number/factor1 print(f'{factor1}*{factor2:0.0f}={number}') break factor1+=1 else: print(f'{number} is a prime number') number+=1
defprint_lines(file_name): print('Here are the people listed in the file:') withopen(file_name) as file: for line in file: line = line.strip() # 判断不是空行并且包含逗号 if line and','in line: name, gender = line.split(',') last_name, first_name = name.split() gender = 'Mr.'if gender.strip() == 'M'else"Mrs" print(f"{gender}{first_name}{last_name}")
if __name__ == '__main__': try: space_number = int(input("Enter a positive integer (possibly 0): ")) symbol = input("Input a symbol: ").removesuffix("\n") if space_number < 0or symbol isNone: raise ValueError except ValueError: print("Input is not an positive integer, giving up.") sys.exit() file_name = input('Input the name of a file in the working directory: ').removesuffix('\n') ifnot exists(file_name): print('Incorrect directory, giving up.') sys.exit()
if __name__ == '__main__': seed_value = int(input("Input a seed for the random number generator: ")) element_number = int(input("How many elements do you want to generate? "))
res = [] seed(seed_value) lst = [randint(0, 20) for _ inrange(0, element_number)] print(lst)
print( f"There is {first_interval} element between 0 and 4."if first_interval > 0else"There is no element between 0 and 4.") print( f"There is {second_interval} element between 5 and 9."if second_interval > 0else"There is no element between 5 and 9.") print( f"There is {third_interval} element between 10 and 14."if third_interval > 0else"There is no element between 10 and 14.") print( f"There is {forth_interval} element between 15 and 19."if forth_interval > 0else"There is no element between 15 and 19.")