跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 这个 Python 初级代码的错误在哪里? 求大神指导!
未分類
19 1 月 2020

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

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

資深大佬 : chanelleemr 7

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)

  • 資深大佬 : flyhelan

    代码贴全了?

  • 資深大佬 : xiri

    额,,,你先学一下 markdown 语法怎么发代码吧,这格式全乱了
    没有细看你的实现,单就你的需求,python 中有 max 和 min 函数可以直接使用就行了。

  • 資深大佬 : aureole999

    看不清楚。
    大概是 largest = larger(largest, fnum) 同理 smaller

  • 資深大佬 : Evrins

    largest 和 smallest 的值没有更新呀,function 传参数是传值,不是传引用,function 里面的 largest, smallest 并不指向前面定义的 largest, smallest

  • 資深大佬 : black11black

    很明显是把 py 当 c 写了

  • 主 資深大佬 : chanelleemr

    @Evrins 谢谢!问题已解决

  • 主 資深大佬 : chanelleemr

    @Evrins
    @aureole999
    问题已解决,谢谢二位

  • 資深大佬 : gzfrankie

    圈子问题加我国特色。

    你看看 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

    会错帖子了……

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具