#Loop
#for loop
fruits = [ "Apple","Peach","Pear"]
for fruit in fruits :
print(fruit)
print(fruit + " pie")
print(fruits)
print(fruits)
#5.1 Average Height
student_heights = input("Input a list of student heights").split()
for n in range(0,len(student_heights)):
student_heights[n] = int(student_heights[n])
print(student_heights)
#Method 1
number_of_students = len(student_heights)
total_height = sum(student_heights)
average_height = total_height/number_of_students
print(average_height)
#Method 2
number_of_students = 0
total_height = 0
for n in student_heights:
number_of_students += 1
total_height += n
average_height = round(total_height / number_of_students)
print(total_height)
print(average_height)
#Method 3
total_height = 0
for height in student_heights:
total_height += height
number_of_students = 0
for student in student_heights:
number_of_students += 1
average_height = round(total_height / number_of_students)
print(average_height)
#5.2 Highest Score
student_scores = input("Input a list of student scores").split()
print(student_scores)
for n in range(0, len(student_scores)):
student_scores[n] = int(student_scores[n])
print(student_scores)
#TypeError: 'str' object does not support item assignment
#字符串为不可变类型,没有办法单独修改其中的值,需要将其转换成列表。即line52 split()
#Method 1
highest_score = max(student_scores)
lowest_score = min(student_scores)
print(f"The highest score in the class is : {highest_score}")
print(f"The lowest score in the class is : {lowest_score}")
#Method 2
highest_score = 0
for score in student_scores:
if score > highest_score:
highest_score = score
print(f"The highest score in the class is : {highest_score}")
#For Loop with Range
for number in range(1,11,3):
print(number)
total = 0
for number in range(1,101):
total += number
print(total)
#5.3 Adding Events
total = 0
for event_number in range(2, 101, 2):
total += event_number
print(total)
total2 = 0
for number in range(1,101):
if number % 2 ==0:
total2 += number
print(total2)
#5.4 Fizz Buzz
for number in range(1,101):
if number % 3 == 0 and number % 5 == 0:
print("FizzBuzz")
elif number % 3 ==0:
print("Fizz")
elif number % 5 == 0:
print("Buzz")
else:
print(number)
#Project_Creat a Password Genarator
import random
letters = ['a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G',
'H','I','J','K','L','M','N','O','P','Q','R',
'S','T','U','V','W','X','Y','Z']
numbers = ['0','1','2','3','4','5','6','7','8','9']
symbols = ['!','@','#','$','%','^','&','*','(',')','_','+']
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input("How many symbols would you like?\n"))
nr_numbers = int(input("How many numbers would you like?\n"))
#Easy Level
password = " "
#for 循环不能迭代数值类型:for nr_letters >0:
for char in range(1,nr_letters+1):
password += random.choice(letters)
for char in range(1,nr_symbols +1):
password += random.choice(symbols)
for char in range(1,nr_numbers + 1):
password += random.choice(numbers)
print(password)
#Hard Level
password_list = []
for char in range(1,nr_letters+1):
password_list.append(random.choice(letters))
for char in range(1,nr_symbols+1):
password_list += random.choice(symbols)
for char in range(1,nr_numbers+1):
password_list += random.choice(numbers)
print(password_list)
random.shuffle(password_list)
print(password_list)
password = ""
for char in password_list:
password += char
print(f"Your password is : {password}")
Day5_Udemy Python 100days
于 2023-08-08 12:56:05 首次发布