简体   繁体   English

遇到一些关于python的列表的麻烦

[英]Having trouble with some lists on python

def main():
    num_list = []
    n = input('Your favorite number: ')

    again = 'g'

    while again == 'g':
        value = float(input('Enter a number: '))
        if value > n:
            num_list.append(value)
        print ('Would you like to enter another number?: ')
        again = input('y=yes, n=no')
main()

Here's my code. 这是我的代码。 The > isn't working, what do i do? >不起作用,我该怎么办?

I see two problems with your code, 我发现您的代码有两个问题,

  1. As comments says, your comparing a float type to a str . 如评论所述,您将float类型与str进行比较。 Note that it is possible in Python 2 to compare mixed types for nonsensical answers, not in Python 3. 请注意,在Python 2中可以比较混合类型以获得无意义的答案,而在Python 3中则不能。

  2. You're comparing your again variable against 'g' only. 您仅将您的again变量与“ g”进行比较。 which is clearly not what you want. 这显然不是您想要的。 Try this code : 试试这个代码:

     def main(): num_list = [] n = input('Your favorite number: ') again = 'g' while again in {'g','y'} : value = float(input('Enter a number: ')) if value > float(n): num_list.append(value) print ('Would you like to enter another number?: ') again = input('y=yes, n=no') main() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM