如何检查在一个范围内哪些数字的十位和个位上有相同的数字? (Python)

huangapple go评论66阅读模式
英文:

How to check what numbers in a range have the same digit in the ones and tens place? (Python)

问题

I'm new to coding. So far I've learned if statements and loops. I have a homework assignment to check which numbers between a range of 10 and 100 have the same numbers in the tens and ones place (11, 22, etc). I'm thinking if I can test which numbers are divisible by 11 I can generate the list I want, however I'm struggling on getting the program to test numbers in a range and give the output. I'm not even sure if I'm on the right track. Looking up things online hasn't helped either.

I'm not looking for anyone to do my homework for me, I just need a pointer in the right direction for what to do.

# 这是我尝试的一些代码,基于我在网上找到的一些东西,但它只不断输出"Error"
for i in range(10, 100):
    count = 0
    if i % 11 == 0:
        count = count + 1
    else:
        print("Error.")
英文:

I'm new to coding. So far I've learned if statements and loops. I have a homework assignment to check which numbers between a range of 10 and 100 have the same numbers in the tens and ones place (11, 22, etc). I'm thinking if I can test which numbers are divisible by 11 I can generate the list I want, however I'm struggling on getting the program to test numbers in a range and give the output. I'm not even sure if I'm on the right track. Looking up things online hasn't helped either.

I'm not looking for anyone to do my homework for me, I just need a pointer in the right direction for what to do.

#This is something I tried based on something I found online, and it just output "Error" over and over
for i in range (10,100):
    count = 0
    if i%11==0:
        count=count+1
    else:
        print("Error.")

答案1

得分: 1

首先,欢迎进入编程的世界!

考虑到你刚刚开始,我只想说,你在某些时候可能会遇到困难,甚至感到卡壳,但要知道,几乎每个人都经历过这些阶段,也许在与编程相关的领域比其他领域更常见。说到这一点,如果你坚持下去,最终你将拥有一项了不起的技能,可以让你创造而不是消费!

看起来你已经解决了你的问题,但为了给你另一种解决方案,你可以使用。所以,你有一百万种不同的方法可以做到这一点,但从纯数学的角度来看(即不使用Python中的特殊“集成”方法),你可以使用取模运算符(%)并将值除以10。

通过获得你的“i”和10的模数的答案,你可以找到数字的最右边的数字 => myNuber = i % 10(例如 56 % 10 = 6)

然后,将你的数字除以十,以去除最右边的数字 => 56 / 10,在这个例子中将得到 5(不要四舍五入!)

基本上,现在你有两个不同的值,分别是 5 和 6(对于 11 和 22 来说是 1 和 1 或 2 和 2)。嗯,我想你可以从这里解决剩下的部分 如何检查在一个范围内哪些数字的十位和个位上有相同的数字? (Python)

祝你在编程之旅中好运!

英文:

First of all, welcome to programming!

Considering you just started, just gotta say that you will always stumble and might even feel stuck at some points, but just know that literally everyone goes through said phases, probably even more so in programming related fields than other ones. With that being said, if you push through it you will eventually have an amazing skill that allows you to produce rather than consume!

It seems you have solved your issue, but to give you another solution you could have used. So, you have a million different ways that you could do this, but an interesting way that you could do this from a purely mathematical point (i.e. not using a special "integrated" method in python) is to use the modulo operator (%) and dividing the value by 10

By getting the answer of your "i" and modulo 10, you can find the rightmost digit of your number => myNuber = i % 10 (e.g. 56 % 10 = 6)

After that, divide your number by ten to remove that rightmost digit => 56 / 10 which (in this example) will give you 5 (Never round up!)

Basically, now you have two different values, the 5 and the 6 (1 and 1 or 2 and 2 for 11 and 22). Well, I think you got the rest from here 如何检查在一个范围内哪些数字的十位和个位上有相同的数字? (Python)

Good luck on your programming journey!

huangapple
  • 本文由 发表于 2023年2月27日 04:12:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定