按照视频教授进度写的代码,有很多需要改进的地方,欢迎大家提出改进意见和交流。课程链接:https://www.bilibili.com/video/av84534946?p=29
# 练习题:猜年龄,三次机会。若三次不中,询问是否继续。回答Y or y,则继续。
import random
age_rand = random.randint(10, 15)
count = 0
while True:
while count < 3:
count += 1
age_guess = int(input("Please input your guess: "))
if age_guess < age_rand:
print("Try bigger")
elif age_guess > age_rand:
print("Try smaller")
elif age_guess == age_rand:
print("Bingo!")
exit()
else:
another_try = input("Do you want another try(y/n): ")
if another_try == "y" or another_try == "Y":
count = 0
continue # 我一直卡在这里,没有加continue
else:
print("Game over!")
exit()
```python
#使用while 循环输出100-50,从大到小,如100,99,98…,到50时再从0循环输出到50,然后结束
n = 100
count = 0
while count < 102:
if n > 49:
print(n)
n -= 1
count = count + 1
else:
n = 0
while n < 50:
print(n)
n = n + 1
count = count + 1
```python
# 使用while循环实现输出2-3+4-5+6…+100 的和
a = 1
sum1 = 0
count = 0
while count < 99:
a = a + 1
if a % 2 != 1:
sum1 = sum1 + a
count += 1
else:
sum1 = sum1 - a
count += 1
print(sum1)
# 输入一年份,判断该年份是否是闰年并输出结果。(编程题)(1) 能被4整除但不能被100整除。 (2) 能被400整除。
yr_1 = int(input("请输入任意年份(非正确数字将终止程序): "))
if (yr_1 % 4) == 0 and yr_1 % 100 != 0 or yr_1 % 400 == 0:
print("是闰年")
else:
print("不是闰年")
# 假设一年期定期利率为3.25%,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?(编程题)
r = 0.0325
inter = 0
y_n = 0
cap_add_int = 10000
while inter < cap_add_int:
y_n += 1
inter += 10000 * r
print(y_n, inter)
# 双色球彩票 选购程序
# 先让用户依次选择6个红球,再选择2个蓝球,最后统一打印用户选择的球号。
# 确保用户不能选择重复的,选择的数不能超出范围。
print("---------欢迎来购双色球彩票,请按提示进行选购----------")
no_of_choice = 0
r_ball_list = []
b_ball_list = []
while no_of_choice < 8:
while no_of_choice < 6:
no_picked = int(input("选择红色球号码: "))
if 1 < no_picked < 33:
if no_picked not in r_ball_list:
no_of_choice += 1
print("第%d个红色球为[%d]" % (no_of_choice, no_picked))
r_ball_list.append(no_picked)
else:
print("该号码已存在")
continue
else:
print("该号码不在选择范围")
continue
else:
no_picked = int(input("选择蓝色球号码: "))
if 1 < no_picked < 33:
if no_picked not in b_ball_list:
no_of_choice += 1
print("第%d个蓝色球为[%d]" % (no_of_choice-6, no_picked))
b_ball_list.append(no_picked)
else:
print("该号码已存在")
continue
else:
print("该号码不在选择范围")
continue
print("红色球选中号码为: ", r_ball_list)
print("蓝色球选中号码为: ", b_ball_list)
print("Good Luck")