【问题标题】:Typecasting in Python to Integer在 Python 中将类型转换为整数
【发布时间】:2018-05-06 14:04:01
【问题描述】:
L = int(input())  
N = int(input())  
W, H = int(input().split(" "))  
for num in range(N):    
    if (W < L or H < L):    
        print("UPLOAD other")][1]

https://i.stack.imgur.com/xgtoS.jpg

【问题讨论】:

  • 请添加您的回溯的文本,而不是图像。
  • 各位代码是完整的,你可以通过点击上面弹出图像的代码来查看完整的代码+错误
  • @cᴏʟᴅsᴘᴇᴇᴅ 添加少量代码部分时出现问题,因此我嵌入了图像
  • @cᴏʟᴅsᴘᴇᴇᴅ 无论如何我明白了,谢谢

标签: python string integer typecasting-operator


【解决方案1】:

您正在尝试将 2 个数字的列表转换为 int。 int only takes a number or a string as its argument.

你想要的是map列表中每个项目的int函数。

>>> w, h = map(int, input().split(" "))
5 10
>>> w
5
>>> h
10

【讨论】:

  • 这比列表/生成器表达式更可取:这值得商榷,但将内置函数应用于列表map() 更简洁。话虽如此,两者都会起作用。此外,在这种情况下,split() 应该等于或优于 split(" ")
【解决方案2】:

int(...) 构造一个整数,它不能被解包到一个元组W, H。你可能想要的是

W, H = (int(x) for x in input().split(" "))

【讨论】:

    猜你喜欢
    • 2016-03-29
    • 1970-01-01
    • 2020-12-29
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多