栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

练习3---python脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

练习3---python脚本

1.第一题

hw3_1.py

try:
    num = int(input('please input a number:'))
    if num % 2 == 0:
        print('It is an even number.')
    else:
        print('It is an odd number.')
except ValueError:
    print('Wrong input!')

运行: python3 hw3_1.py
(python2中input()输入的时候会自动判别数据类型,而python3中的input()默认输入的都是string类型的,python2如果也想这样可以用raw_input()) 

2.第二题 

hw3_2.py 

import random
secret = int(random.uniform(0,10))
print("I'm thinking of a number between zero and ten.", 'Can you guess what it is?')
guess=11
while guess != secret:
    try:
        guess = int(input("Take a guess: "))
    except ValueError:
        print("Wrong input! Please input a number!")
print("Well done!")

运行: python3 hw3_2.py 

 3.第三题

hw3_3.py

while True:
    try:
        score = float(input('Enter score: '))
        if score >= 0.0 and score <= 1.0:
            if score >= 0.9:
                print('A')
            elif score >= 0.8:
                print('B')
            elif score >= 0.7:
                print('C')
            elif score >= 0.6:
                print('D')
            else:
                print('F')
        else:
            print('Bad score')
    except ValueError:
        print('Bad score')

运行: python3 hw3_3.py  

4.第四题

hw3_4.py

count = 0
total = 0
while True:
    num = input('Enter a number: ')
    if num == 'done':
        print(total, count, total / count)
        break
    try:
        total += int(num)
        count += 1
    except ValueError:
        print('Invalid input')

运行: python3 hw3_4.py   

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/461325.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号