循环和函数(上)练习

本文提供了一系列编程练习,涵盖了解一元二次方程、加法游戏、预测未来日期、整数排序、价格比较、计算月份天数、猜硬币、剪刀石头布游戏、选牌游戏以及判断回文数和计算三角形周长等任务,旨在提升编程技能和逻辑思维能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、(代数方面:解一元二次方程)例如:ax²+bx+c=0的平方根可以使用下面的公式获取
r1=(-b+sqrt(b²-4ac))/2a,r2=(-b-sqrt(b²-4ac))/2a
b²-4ac称为二次方程的判别式,为正则有两个实根,为0有一个根,为负没有根。
编写程序,提示用户输入a.b.c的值,显示判别式结果,然后显示根,没有根则输出
The equation has no real roots.

import math
def zhi(a,b,c):
    pan = b * b - 4 * a * c
    if pan > 0:
        r1 = (-b + math.sqrt(pan)) / 2 * a
        r2 = (-b - math.sqrt(pan)) / 2 * a
        print("The roots are %.6f and %.5f"%(r1,r2))
    elif pan == 0:
        r = (-b + math.sqrt(pan)) / 2 * a
        print("The root is %.0f"%r)
    else:
        print("The equation has no real roots")
        
def Start():
    print("请输入a.b.c的值")
    a = int(input("a:"))
    b = int(input("b:"))
    c = int(input("c:"))
    zhi(a,b,c)
Start()
请输入a.b.c的值
a:1
b:3
c:1
The roots are -0.381966 and -2.61803

请输入a.b.c的值
a:1
b:2
c:1
The root is -1

请输入a.b.c的值
a:1
b:2
c:3
The equation has no real roots

2、(游戏:学习加法)编写一个程序产生两个100以下的整数,然后提示用户输入这两
个整数的和。如果答案正确,程序报告结果为真,否则为假

import random

def one():
    a = random.randint(0,100)
    print("第一个数是:%d"%a)
    return a

def two():
    b = random.randint(0,100)
    print("第二个数是:%d"%b)
    return b

def Start():
    o = one()
    t = two()
    you = int(input("请输入两个整数的和:"))
    he = o + t
    if you == he:
        print("True")
    else:
        print("False")
Start()
第一个数是:33
第二个数是:99
请输入两个整数的和:56
False

第一个数是:63
第二个数是:6
请输入两个整数的和:69
True

3、(找未来数据)编写程序提示用户输入表示今天是一周内那一天的数字(星期天0,
星期一1…)还要提示用户输入今天之后到未来某天的天数,显示这天是星期几

def week(day,fut):
    days = ['Sunday','Monday','Tuesday','Wendnesday','Thursday','Friday','Saturday']
    a = days[day]
    b = days[(day + fut) % 7]
    print("Today is %s and the future day is %s"%(a,b) )

def Start():
    day = int(input("Enter today's day:"))
    fut = int(input("Enter the number of days elapsed since today:"))
    week(day,fut)
Start()
Enter today's day:1
Enter the number of days elapsed since today:3
Today is Monday and the future day is Thursday

4、(第三个整数排序)编写一个程序提示用户输入三个整数,然后升序显示他们

def shu(a,b,c):
    d = [a,b,c]
    d.sort(reverse = False)
    print(d)
def Start():
    a = int(input("请输入第一个整数:"))
    b = int(input("请输入第二个整数:"))
    c = int(input("请输入第三个整数:"))
    shu(a,b,c)
Start()
请输入第一个整数:2
请输入第二个整数:1
请输入第三个整数:3
[1, 2, 3]

5、(金融方面:比较价钱)假设你购买大米时发现它有两种包装。编写一个程序比较两种包
装的价钱。程序提示用户输入每种包装的重量和价钱,然后显示价钱更好的那种包装。

def contrast(money1,weight1,money2,weight2):
    dan1 = money1 / weight1
    dan2 = money2 / weight2
    if dan1 > dan2:
        print("第二种大米价钱更好")
    elif dan1 < dan2:
        print("第一种大米价钱更好")
    else:
        print("两种大米价格相同")

def Start():
    money1 = float(input("请输入第一种大米的价钱:"))
    weight1 = float(input("请输入第一种大米的重量:"))
    money2 = float(input("请输入第二种大米的价钱:"))
    weight2 = float(input("请输入第二种大米的重量:"))
    contrast(money1,weight1,money2,weight2)
Start()
请输入第一种大米的价钱:10
请输入第一种大米的重量:5
请输入第二种大米的价钱:20
请输入第二种大米的重量:4
第一种大米价钱更好

6、(找出一个月中的天数)编写程序提示用户输入月和年,然后显示这个月的天数

def day(year,month):
    if year % 4 == 0: 
        days = ['null','31','29','31','30','31','30','31','31','30','31','30','31']
        sum = days[month]
    else:
        days = ['null','31','28','31','30','31','30','31','31','30','31','30','31']
        sum = days[month]
    print("%d年%d月份有%s天"%(year,month,sum))
def Start():
    year = int(input("请输入年份:"))
    month = int(input("请输入月份:"))
    day(year,month)
Start()
请输入年份:2007
请输入月份:2
20002月份有28

7、(游戏:头或尾)编写程序让用户猜测一个弹起的硬币显示的是正面还是反面。程序
提示用户输入一个猜测值,然后显示猜测是否正确

import random
def pan(pao,cai):
    print("硬币弹起方向%d"%pao)
    if pao == cai:
        print("恭喜你,答对啦!")
    else:
        print("很遗憾,答错了")

def Start():
    pao = random.randint(1,2)
    cai = int(input("你猜测的结果是[1.正面 2.反面]:"))
    pan(pao,cai)
Start()
你猜测的结果是[1.正面 2.反面]2
硬币弹起方向2
恭喜你,答对啦!

8、(游戏:剪刀,石头,布)编写程序玩剪刀石头布游戏

import random

def bijiao(computer,player):
    print("我的选择%d"%computer)
    if computer == 1 and player == 2:
        print("恭喜你,你赢啦!")
    elif computer == 2 and player == 3:
        print("恭喜你,你赢啦!")
    elif computer == 3 and player == 1:
        print("恭喜你,你赢啦!")
    elif computer == player:
        print("我们是一样哒!")
    else:
        print("很遗憾,你输了")

def Start():
    computer = random.randint(1,3)
    player = int(input("请输入你的选择[1.剪刀 2.石头 3.布]"))
    bijiao(computer,player)
Start()
请输入你的选择[1.剪刀 2.石头 3.]2
我的选择3
很遗憾,你输了

在这里插入图片描述

def yonghu(year,month,days):
    week = ['Saturday','Sunday','Monday','Tuesday','Wendnesday','Thursday','Friday']
    if month == 1:
        month = 13
        year = year - 1
    if month ==2:
        month = 14
        year = year - 1
    a = int(days+((26*(month+1))//10)+(year%100)+((year%100)/4)+((year//100)/4)+5*year//100)%7
    day = week[a]
    print('Day of the week is %s'%day)
def Start():
    year = int(input('Enter year(e.g..2018):'))
    month = int(input('Enter month(1-12):'))
    days = int(input('Enter the day of the month(1-31):'))
    yonghu(year,month,days)
Start()
Enter year(e.g..2018):2013
Enter month(1-12):1
Enter the day of the month(1-31):25
Day of the week is Friday

10、(游戏:选出一张牌)编写程序模拟从52张牌中选出一张。你的程序应该显示这张牌的大小和花色

def daxiao():
    import numpy as np
    pai = np.random.choice(['Ace','2','3','4','5','6','7','8','9','10','Jack','Queen','King'])
    hua = np.random.choice(['梅花','红桃','方块','黑桃'])
    print("The card you picked is the %s of %s"%(pai,hua))

def Start():
    daxiao()
Start()
The card you picked is the 7 of 红桃

11、(回文数)编写程序提示用户输入一个三位数,然后决定它是否是一个回文数。如果
一个数从左向右和从右向左读取时是一样的,那么这个数是回文数

def panduan(shu):
    a = shu % 10 * 100 + shu // 10 % 10 * 10 + shu // 100
    if shu == a:
        print('%d是回文数'%shu)
    else:
        print('%d不是回文数'%shu)
def start():
    shu = int(input('请输入一个三位数: '))
    panduan(shu)
start()
请输入一个三位数: 121
121是回文数

请输入一个三位数: 123
123不是回文数

12、(计算三角形的周长)编写程序读取三角形的三个边,如果合法则计算周长,否则显示非法的

def s(a,b,c):
    if a < b + c and b < a + c and c < a + b:
        sum = a + b + c
        print("三角形的周长是:%d"%sum)
    else:
        print("您输入的边长是非法的")

def Start():
    a = int(input("请输入三角形的边长:"))
    b = int(input())
    c = int(input())
    s(a,b,c)
Start()
请输入三角形的边长:3
4
5
三角形的周长是:12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值