Python代码不打印任何内容,只给出代码0

2024-04-24 19:06:19 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我很茫然,我在文件夹里找到了这个python代码,因为我不久前就开始学习python了,今天的课我需要这个代码。问题是,它没有打印任何东西,只是表明它没有问题。你能帮助我吗?我需要sc的代码和sc的输出,如果你能告诉我什么样的代码我失踪或任何东西真的。。谢谢

def square(n):
    word = int(raw_input('Enter number here: '))
    if len(word) > 0:
        squared = n ** 2
        print ("%d squared is %d" %(n,squared))

Tags: 代码文件夹numberinputrawlenifhere
2条回答

首先,使用python3,需要用input替换raw_input。其次也是最重要的一点,integer不适用于len函数,您应该直接比较整数。要处理潜在的类型不匹配,请使用以下代码(可以将其放入循环或进行任何其他修改)

def square():
    n = input('Enter number here: ')
    try:
        n = int(n)
    except TypeError:
        print("Input is not a number")
    else:
        if word > 0:
            squared = n ** 2
            print ("%d squared is %d" %(n,squared))

# Let's call the function
square()

顺便说一句,我认为调用整数变量word不是很好的自描述性。你知道吗

我认为这会奏效:

def square(n):
    number = int(input('Enter number here: '))
    if number > 0:
        squared = n ** 2
        print ("%d squared is %d" %(n,squared))

相关问题 更多 >