Vu python week 3,4 - Selection and Repetition Structures

Week 3,4 - Selection and Repetition Structures

没啥特别说的知识点,就刷Vu上的文档吧。

Lab-4 Nicks2019partA

Ex1:

for count in range (3):
    print (count)

运行结果:

0
1
2

解析:如果括号里为一个数a,则依次输出0 — (a-1)这a个数字。

Ex2:

for count in range (1,3):
    print (count)

运行结果:

1
2

解析:如果括号里为(a,b)则输出a ~ (b~1)这(b - a)个数字。

Ex3:
输入一个上限upbound和下限lowbound,求这之间所有数的和

upbound = int(input ("Please enter the upbound :"))
lowbound = int(input ("Please enter the lowbound:"))
total = 0
for count in range (lowbound,upbound+1):
    total= total + count
print ("Total is "+str(total))

样例输出:

Please enter the upbound :100
Please enter the lowbound:1
Total is 5050

Ex4:
输入密码,只有3次机会。若均错误及锁住此账户。

password = "whh"
word = ""
for count in range (3):
    word = input("Please enter your password:")
    if word == password:
        break
    else:
        print ("Your password is error ,you have",str(2-count),"chances")
if word == password:
    print ("Success")
else:
    print ("No more chances!")

测试输入:

Please enter your password:fs
Your password is error ,you have 2 chances
Please enter your password:hg
Your password is error ,you have 1 chances
Please enter your password:whh
Success

Ex5:
输入一个数number,再输入行数row。输出数字number的row行乘法列表。

number = int (input ("Please input a whole number :"))
row = int (input("How many rows in the table :"))
for count in range (1,row+1):
    ans = number * count
    print ('%4d%3s%4d%3s%4d'%(count,"x",number,"=",ans))

测试输入:

Please input a whole number :6
How many rows in the table :6
   1  x   6  =   6
   2  x   6  =  12
   3  x   6  =  18
   4  x   6  =  24
   5  x   6  =  30
   6  x   6  =  36

Ex6:
用while语句代替如下代码:

for count in range (5):
    print (count)

如下:

x = 0
while x<5:
    print (x)
    x += 1

Ex7:
将 Ex 4 用while语句写:

password = "whh"
word = ""
x = 0
while x < 3:
    word = input("Please enter your password:")
    x += 1
    if word == password:
        break
    else:
        print ("Your password is error ,you have",str(3-x),"chances")
if word == password:
    print ("Success")
else:
    print ("No more chances!")

Lab-4 Nicks2019partB

Ex 9,10:
此例题想要考察try ,except语句来提示变量错误。下程序我们可以看到当用户输入数据类型错误时,可以提示错误:

lower = 1
upper = 10
while True :
    try :
        num = int (input ("Please enter a number :"))
    except ValueError:
        print ("Your number type is Error , please enter again:")
    else:
        if lower <= num <= upper:
            break
        else:
            print ("Your number should be between 1 and 10")
if num == upper:
    print ("upper limit!")
elif num == lower:
    print ("lower limit!")
else:
    print ("In range!")

Ex 11,12:
python 中 random()函数的应用:

import random
print( random.randint(1,10) )        # 产生 1 到 10 的一个整数型随机数
print( random.random() )             # 产生 0 到 1 之间的随机浮点数
print( random.uniform(1.1,5.4) )     # 产生  1.1 到 5.4 之间的随机浮点数,区间可以不是整数
print( random.choice('tomorrow') )   # 从序列中随机选取一个元素
print( random.randrange(1,10,2) )   # 生成从1到10的间隔为2的随机整数(1,3,5,7,9)

a=[1,3,5,6,7]                # 将序列a中的元素顺序打乱
random.shuffle(a)
print(a)

Ex 13:
猜数字小游戏

import random
ans = random.randint(1,10)
for count in range (3):
    num = int(input("Guess a number :"))
    if num > ans:
        print ("Your number is too high")
    if num < ans:
        print("Your number is too low")
    if num == ans:
            break
if num == ans :
    print ("Success!")
else:
    print ("You don't have a chance ,the number is",ans)

测试输入:

Guess a number :5
Your number is too low
Guess a number :8
Success!

Ex 14:
综合测试:
输入存款金额,选择账户类型,输入存款年限。计算每年的利息,总钱数,并格式化输出。

amount = float(input("Enter the deposit amount :"))
if amount < 0:
    amount = float(input("Amount should greater than 0,enter the deposit amount again:"))
type = str(input("Enter the deposit type(vip/staff/reg):"))
term = int (input("Enter the deposit term (number of years):"))
print ()
print ("%4s%20s%20s"%("Year","Interest","Ending balance"))
interest = 0
sum =amount
total_interest = 0
for count in range (term):
    if type == "reg"  :
        interest = sum * 0.2
        sum = sum + interest
        total_interest = total_interest + interest
    if type == "vip" or  type == "staff" :
        interest = sum *0.3
        sum = sum +interest
        total_interest = total_interest +interest
    print("%4d%20.2f%20.2f"%(count+1,interest,sum))
print()
if type == "reg":
    print("Fixed deposit rate :$0.02")
if type == "vip" or type == "staff" :
    print("Fixed deposit rate :$0.03")
print(f"Ending balance :$%0.2f"%sum)
print("Total interest earned :$%0.2f"%total_interest)

测试样例:

Enter the deposit amount :50000
Enter the deposit type(vip/staff/reg):reg
Enter the deposit term (number of years):4

Year            Interest      Ending balance
   1            10000.00            60000.00
   2            12000.00            72000.00
   3            14400.00            86400.00
   4            17280.00           103680.00

Fixed deposit rate :$0.02
Ending balance :$103680.00
Total interest earned :$53680.00

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值