从0开始学python:简单的练习题3

python 复制代码
import random
def temp(x):
    if x<=37.5:
        print(f"您的体温是:{x},体温正常请进")
    else:
        print(f"您的体温是:{x},需要隔离")
x=random.randint(35,45)
temp(x)

python 复制代码
def find():
    """
    查询余额函数
    直接查询全局变量money的值并返回
    :return:
    """
    global money
    print(f"账户还有:{money}")
    return money
def store(storemoney):
    """
    实现存款操作
    :param storemoney: 本次需要存入的金额
    :return: 存款后账户内的钱
    """
    global money
    money=storemoney+money
    print(f"存钱后的账户金额为:{money}")
    return money
def out(outmoney):
    """
    实现取款操作
    :param outmoney:取出的金额
    :return: 取出后的金额
    """
    global money
    if money>=outmoney:
        money=money-outmoney
        print(f"取钱后的账户金额为:{money}")
    else:
        print("金额不足")
    return money
def main():
    """
    实现页面的主函数
    :return:
    """
    step1=input("当前需要执行的操作:")
    while True:
        if step1=='查询余额':
            find()
            main()
        elif step1=='存款':
            storemoney=input("此次存款的金额:")
            storemoney=float(storemoney)
            store(storemoney)
            main()
        elif step1=='取款':
            outmoney=input("此次取款的金额:")
            outmoney=float(outmoney)
            out(outmoney)
            main()
        else:
            print("欢迎下次使用")
        break
money=5000000
name=input("请输入你的姓名:")
main()

在该程序中,要注意break语句的位置,决定结束的是哪一个循环,在函数中,如果要使用或者修改全局变量的值,要在函数中用global声明该全局变量,即global xxx,即可正常使用。

相关推荐
咩咩啃树皮7 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
阳光是sunny8 小时前
LangGraph中的Reducer是什么
前端·人工智能·后端
触底反弹8 小时前
一文搞懂 Tailwind CSS 弹性布局:从原理到实战
前端·css·html
灯澜忆梦8 小时前
GO_并发编程---定时器
开发语言·后端·golang
阳光是sunny8 小时前
从链到图:LangGraph 入门基础全解析
前端·人工智能·后端
-银雾鸢尾-8 小时前
C#中的StringBuilder相关方法
开发语言·c#
小林ixn8 小时前
从 ??= 到 onKeyDown:一个 React 组件的“自我修养”
前端·javascript·react.js
-银雾鸢尾-8 小时前
C#中结构体与类的区别;抽象类与接口的区别
开发语言·c#
REDcker9 小时前
显示分辨率标准对照详解
前端·网络·分辨率·显示·屏幕
এ慕ོ冬℘゜9 小时前
前端实战:jQuery 多条件联合搜索(标题模糊查询 + 日历时间段筛选)
前端·javascript·jquery