简体   繁体   English

我在这里没有看到我的错误 - 尝试学习 Python

[英]I don't see my error here - Trying to learn Python

I'm trying to become comfortable with python.我正在努力适应 python。 I've been trying some simple activities that I've given in my beginning c++ classes when I was teaching.我一直在尝试一些简单的活动,这些活动是我在教学时在开始的 C++ 课程中提供的。 I did one involving functions and writing a file which worked flawlessly.我做了一个涉及函数和编写一个完美工作的文件。 I thought this one would be easier.我以为这个会更容易。 It acts like it is in a silent endless loop, but it won't even let me trace it.它就像处于一个无声的无限循环中,但它甚至不会让我追踪它。 Can someone see where I am going awry?有人能看到我哪里出错了吗?

# Find Adam Numbers

def isAdamNumber(candidate):
    isAdam = False
    rev = reverse(candidate)
    square = candidate * candidate
    revsq = rev*rev
    if revsq == reverse(square):
        isAdam = True
    return isAdam
    
def reverse(num):
    rev=0
    while num > 0:
        rev = rev * 10 + num%10
        num/=10
    return rev
for x in range (11,25):
    if isAdamNumber(x):
        print(x, " is an adam number\n")

快速修复是将/=更改为整数除法版本, //=

Inside the reverse function, you are going into an infinite loop.在 reverse 函数中,您将进入一个无限循环。 num value always will be greater than 0, therefore the while loop will continuously run. num 值总是大于 0,因此 while 循环将持续运行。 In python, you can get the reverse of the function without much effort.在python中,你可以毫不费力地得到函数的反转。 Convert the integer to string and reverse the string and now change the string back to integer.将整数转换为字符串并反转字符串,现在将字符串更改回整数。

def reverse(num):
    num_str = str(num)[::-1]
    return int(num_str)

I think this function definition can solve your problem.我认为这个函数定义可以解决你的问题。 To visualize the python to learn and teach, use this link要可视化 Python 以进行学习和教学,请使用此链接

num > 0 is never False . num > 0永远不会是False Dividing a positive number by 10 repeatedly makes it smaller, but it never becomes zero, so the while loop keeps repeating.将一个正数反复除以 10 会使它变小,但它永远不会变为零,因此while循环不断重复。

Use //= instead.使用//=代替。 It rounds to the nearest integer, so it will reach 0.它四舍五入到最接近的整数,因此它将达到 0。

This also wouldn't reverse numbers (unless I'm missing something). 这也不会反转数字(除非我遗漏了一些东西)。 Alternatively, you can use或者,您可以使用

int(str(num)[::-1])

which converts the number to a string, reverses it using slicing , and turns it back into an integer.它将数字转换为字符串,使用slicing将其反转,然后将其转换回整数。

The problem has already been addressed by the other answers, so here's the expanded and simplified version of the slicing that's going on [this doesn't actually use slicing]:其他答案已经解决了这个问题,所以这里是正在进行的切片的扩展和简化版本[这实际上并不使用切片]:

def reverse(num):
    rev = ''
    num = str(num)
    for i in range(len(num) - 1, -1, -1):
        rev += num[i]
    return int(rev)

This counts backward from the last element in the string version of num , and adds all the elements of num (in reverse order) to rev.这从num的字符串版本中的最后一个元素向后计数,并将num所有元素(以相反的顺序)添加到 rev。

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

相关问题 我似乎不明白这里的错误 - i don't seem to understand the error here 我用 python 中的 pygame 制作的 FlappyBird 的 DQN 不要学习 - My DQN for a FlappyBird made with pygame in python Don't learn 试图用python继承来理解这里简单的东西的根本区别,而我什至不知道该怎么称呼它 - Trying to understand the fundamental difference on something simple here with python inheritance vs i don't even know what to call this Python:我的 if 语句不起作用但我没有收到任何错误? - Python: My if statement is not working but I don't get any error? Python:我的代码出错,我不知道如何修复它 - Python: Error in my code and i don't know how to fix it 第1行的PYTHON-2.x语法错误,但我没有看到任何? - PYTHON-2.x Syntax error on line 1 but i don't see any? pyBluez python服务器-电话看不到我的服务器 - pyBluez python server - phone don't see my server 尝试学习如何自己计算方差,但是不了解此错误 - Trying to learn how to calculate variance by myself, however don't understand this error 我的 twilio 消息被发送,但我没有看到消息 - My twilio message gets send but i don't see the message 显然是我的应用。 跑了,但是我什么都没看到 - Apparently my app. runs but I don't see anything
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM