V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chanelleemr
V2EX  ›  Python

这个 Python 初级代码的错误在哪里? 求大神指导!

  •  
  •   chanelleemr · 2020-06-22 20:46:29 +08:00 · 2402 次点击
    这是一个创建于 1400 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Cousera 上的一个课程作业,目的是允许用户连续输入几个整数、比较它们的大小。

    这里我定义了两个函数来进行比较,结果显示一直是 none,不知道是哪一步错了,望各位不吝赐教

    initiate the min/max value

    largest = None smallest = None

    we could define the function of comparison with def()

    def larger(largest, fnum): if largest is None: largest = fnum else: if largest < fnum: largest = fnum return smallest

    def smaller(smallest, fnum): if smallest is None: smallest = fnum else: if smallest > fnum: smallest = fnum return smallest

    use while to make loop

    while True:

    let user input the numbers

    snum = input('Enter a number: ')
    

    insert the if (done)

    if snum == "done":
        break
    

    Transfer to float type

    we use try/except to aviod explosion

    try:
        fnum = int(snum)
    except:
        print ("Invalid input")
        continue
    

    make comparison (MAX)

    larger(largest, fnum)
    

    make comparison (MIN)

    smaller(smallest, fnum)
    

    print the result

    print ("Maximum is", largest) print ("Minimum is", smallest)

    9 条回复    2020-06-23 02:32:00 +08:00
    flyhelan
        1
    flyhelan  
       2020-06-22 21:07:01 +08:00
    代码贴全了?
    xiri
        2
    xiri  
       2020-06-22 21:09:59 +08:00   ❤️ 1
    额,,,你先学一下 markdown 语法怎么发代码吧,这格式全乱了
    没有细看你的实现,单就你的需求,python 中有 max 和 min 函数可以直接使用就行了。
    aureole999
        3
    aureole999  
       2020-06-22 21:16:08 +08:00   ❤️ 2
    看不清楚。
    大概是 largest = larger(largest, fnum) 同理 smaller
    Evrins
        4
    Evrins  
       2020-06-22 21:29:02 +08:00 via iPhone   ❤️ 1
    largest 和 smallest 的值没有更新呀,function 传参数是传值,不是传引用,function 里面的 largest, smallest 并不指向前面定义的 largest, smallest
    black11black
        5
    black11black  
       2020-06-22 23:09:27 +08:00
    很明显是把 py 当 c 写了
    chanelleemr
        6
    chanelleemr  
    OP
       2020-06-23 02:00:26 +08:00
    @Evrins 谢谢!问题已解决
    chanelleemr
        7
    chanelleemr  
    OP
       2020-06-23 02:01:00 +08:00
    @Evrins
    @aureole999
    问题已解决,谢谢二位
    gzfrankie
        8
    gzfrankie  
       2020-06-23 02:30:57 +08:00 via iPhone
    圈子问题加我国特色。

    你看看 MAGA 哪一家是用 JAVA 主流搭出来的…

    另外你哪里看十手 tiobe,2020 年 6 月 tiobe.com/tiobe-index

    1. C 17.19%
    2. Java 16.10%
    3. Python 8.36%
    4. C++ 5.95%
    5-10 C#、VB 、JS 、PHP 、R 、SQL
    gzfrankie
        9
    gzfrankie  
       2020-06-23 02:32:00 +08:00 via iPhone
    会错帖子了……
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5378 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:58 · PVG 14:58 · LAX 23:58 · JFK 02:58
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.