Python—ex2 and ex3—基础相关 and 控制结构_条件和循环

Python—ex2 and ex3—基础相关 and 控制结构_条件和循环

2019.12.23日

打印出Python中的所有关键字

导入keyword模块,打印kwlist的值。

import keyword
print (keyword.kwlist)

基本类型转换

描述
将以下字符串转换为整数,浮点数和布尔值,并将转换后的值及其类型打印出来‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

floatAsString = ‘5.0’
#your code is here

floatAsString = '5.0'
# 转换为整数
a=int(eval(floatAsString))
# 转换为实数
b=float(floatAsString)
# 转换为bool值
c=bool(floatAsString)

# 打印转换后的整数和它的类型
print(a)
print(type(a))
# 打印转换后的实数和它的类型
print(b)
print(type(b))
# 打印转换后的bool值和它的类型
print(c)
print(type(c))

程序分析

描述
分析下面程序,若输入score为80,输出grade为多少?是否符合逻辑,为什么?
grade = None
score = float(input())
if score >= 60:
grade = “D”
elif score >= 70.0:
grade = “C”
elif score >= 80.0:
grade = “B”
elif score >= 90.0:
grade = “A”

print(grade)
修改以上程序,使得它符合逻辑并正确运行。

grade = None
score = float(input())
if score>=90:
    grade="A"
elif score>=80:
    grade = "B"
elif score>=70:
    grade = "C"
elif score>=60:
    grade = "D"
else:
    grade = None   #,没有这一行就显示只有80分,脑补的None,结果输出100分了。。  
print(grade)

今天是今年的第几天

描述
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

补充完整下面的代码,使得程序输出今天是今年的第几天。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

import time

date = time.localtime()
year, month, day = date[:3]

day_of_month = [31,28,31,30,31,30,31,31,30,31,30,31]
your code is here

import time                #说实话,这个我不会写,看别人的,也没看懂。。。
date=time.localtime()
year,month,day=date[:3]
day_of_month=[31,28,31,30,31,30,31,31,30,31,30,31]
year=int(input())
month=int(input())
day=int(input())
sum=day
i=0
if (year%4==0 and year%100!=0)or(year%400==0):
    day_of_month[1]=29
else:
    day_of_month[1]=28
while i<month-1:
    sum=sum+day_of_month[i]
    i+=1
print(sum)

Python 日期和时间 | 菜鸟教程(Important)

https://www.runoob.com/python/python-date-time.html

奇数和偶数

描述
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

请用户输入一个整数,然后判断这个数是奇数还是偶数,打印结果。‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入输出示例
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入	输出

示例 1
36
36 is even.
示例 2
17
17 is odd.

n=int(input())
if n%2==0:
    print(str(n)+" is even.")   #记得标点符号和转化成str
else:
    print(str(n)+" is odd.")

猜数

描述
1) 猜数游戏,程序随机生成一个[a,b]之间的数,由用户去猜;程序会根据用户所猜测的数,给出提示信息,如:所猜的数过大;所猜的数过小;恭喜猜对了。直到用户猜对,程序才会结束。示例输出如下(所猜数字为[1,100]之间的数):
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫ 2)选做
在程序中加入游戏次数,使得用户根据游戏次数玩多次。示例输出如下(所猜数字为[1,100]之间的数):
‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‫ 提示:所猜数的范围 a 和 b 可以预先指定。

import random                        #导入随机模块
Set_number = random.randint(0,100)   #random.randint(a,b)即随机产生a-b之间的一个数
print(Set_number)
Guess_number = int(input(""))
N = 1
while(Guess_number < Set_number or Guess_number > Set_number):
    N = N + 1
    if Guess_number > Set_number:
        print("遗憾,太大了")
    elif Guess_number < Set_number:
        print("遗憾,太小了")
    Guess_number = int(input("请重新输入猜测的数:"))
print("预测{}次,你猜中了".format(N))
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,100,2) )   # 生成从1到100的间隔为2的随机整数

Python random() 函数 | 菜鸟教程

https://www.runoob.com/python/func-number-random.html

输出200以内能被17整除的最大正整数

for i in range(200, 17,-1):
    if(i%17==0):
        print(i)
        break

完全平方数

描述
一个整数加上100是一个完全平方数,再加168又是一个完全平方数,请问该数是多少?

for i in range(85):
    for j in range(85):
        if i%2==0 and j%2==0 or i%2!=0 and j%2!=0:
            if i**2-j**2==168:
                x=j**2-100
                print(x)
发布了47 篇原创文章 · 获赞 2 · 访问量 1342

猜你喜欢

转载自blog.csdn.net/qq_45550139/article/details/103661520
ex2
ex3