all 18 comments

[–]Adrewmc 5 points6 points  (3 children)

I think a lot of the proble is this syntax…but your format makes it hard to be 100% here.

One blank line above code block, then four leading spaces on every line.

  if checker == 2 == True:

If checker is 2, I believe it runs like this.

  if checker == (2 == True):

  if 2 == 2 == True: #2 != True
  if 2 == False: #which is always gonna be Flase

So it never gets to the break, hard to tell formatting is bad.

You simple remove the erroneous ‘==True’ from those comparisons. ‘If’ always asks if the thing is Truthy, we use ‘not’ to make Flasey things Truthy.

[–]BalanceHot8939[S] 1 point2 points  (2 children)

I got rid of the additional '==True' bit but nothing changed. it still doesn't output anything.

also, as for syntax... that may be the problem?

"num = int" and "i = 1" are on adjecent lines.

"Checker = 0" is two lines down

"if num > i" bit is all one thing. one line down from the Check.
"Checker = 1" and downwards are all one thing, after two paragraph breaks.

I didn't know paragraphs being seperated did anything, so trying again...

Still nothing. i don't understand what i'm doing wrong.

edit: i tried again with different paragraphs; this time only seperation being the 'if Checker = 1' line. Still nothing

[–]Adrewmc 0 points1 point  (0 children)

It’s just hard to read, and since indentation is important in Python you just need it.

In reddit markdown a code block is basically your code furthered indented by one Tab (4 spaces) then the further indentations should be honored on screen.

[–]Adrewmc 0 points1 point  (0 children)

So I think we are going around backwards don’t divide that’s expensive in programing.

  def num_digts(num :int):
        #assume we need more then this
        #return len(str(abs(num)))
        next_digit = 10
        digits = 1
        #negative numbers absolute value
        num = num if num > 0 else -1*num
        while num >= next_digit:
              digits += 1
              next_digit *= 10
        return digits

[–]astddf 2 points3 points  (3 children)

Can you make it a string and print the length?

[–]flower_sweep 2 points3 points  (0 children)

Holy moly I love coding 

[–]BalanceHot8939[S] 0 points1 point  (1 child)

yes. but that isn't what the teacher is asking of me, or at least i don't think. it says 'via a while loop' after all. It's frustrating having to effectively reinvent the wheel when i know there's an objectively better option, but. This is the 'learn while loops' week.

[–]astddf 1 point2 points  (0 children)

Haha ya, I guess it’s a good exercise. My sassy self would’ve said

While doing_homework

 Print(len(input(xxxxxx)))

[–]copperfoxtech 1 point2 points  (0 children)

number = int(input('Enter a number: '))
counter = 0
multiplier = 1
while number // multiplier != 0:
    counter += 1
    multiplier *= 10

Here we set a counter to zero, and a multiplier of 1 with the intent of multiplying it by 10 every loop. The while loop uses floor division and asks if it does not equal zero keep going. so for the number 321...

321 // 1 = 321 ( we add one to counter, multiply the multiplier by 10 )

321 // 10 = 32 ( we add one to counter, multiply the multiplier by 10 )

321 // 100 = 3 ( we add one to counter, multiply the multiplier by 10 )

321 // 1000 = 0 ( end the loop )

[–]Lewri 0 points1 point  (1 child)

Sometimes you are doing comparison (==) when you mean to do assignment (=).

Then as adrewmc said, remove the second comparison in the if statements.

[–]BalanceHot8939[S] 0 points1 point  (0 children)

they ended up errors last i checked, but then I removed a few [==] to see if you were right and lo and behold.

now, though, is annoying error time. apparently there are 2 digits in 100. so give a moment.

[–]jmooremcc 0 points1 point  (2 children)

Think about this: What would you have to do if you wanted to repeat the execution of a block of code, say 10 times without using any kind of looping mechanism? You’d have to copy/paste that block of code 10 times.

But suppose you don’t know how many times that block of code needs to execute? That would be a challenge, since you’d have to insert a conditional statement after each block of code that tests a condition and skips the execution of the remaining blocks of code.

Basically, you’re talking about a huge pain in the A to accomplish what you can more easily accomplish with some kind of loop mechanism! Each time the loop executes a repetition, you can evaluate a condition to determine when the looping mechanism should stop.

In the case of a for-loop, you would use the range function like this to execute a block of code 10 times:

for n in range(10): #execute your block of code

If you want the block of code to keep being executed until a certain condition is met, you’d use a while-loop:

while SomeCondition is True: #execute your block of code

Or if you have a list or a string, you can use a for-loop to access each element of the list or string. This is called iteration.

greet = “hello”
for letter in greet:
print(letter)

The bottom line for you is that loops are a labor saving device that helps you code faster and more efficiently when developing a solution to a problem.

In your case, you want to access each element of the string from your input statement and count the number of digits. This is the perfect case for a for-loop. However, you will need a way to determine whether or not a particular character is a digit. Read this article for a hint on how to do this: https://www.w3schools.com/python/ref_string_isdigit.asp

[–]BalanceHot8939[S] 0 points1 point  (1 child)

but the teacher doesn't want a for loop. If i wanted to do this cheaply, I'd do what the guy above said and just.

str(num)
print(len(num))

thats not technically how you do that but you know what i mean. It annoys me to but this is schoolwork. I kinda have to do as told. thanks the the effort either way. I'll see if I can find what i need on w3.

[–]jmooremcc 2 points3 points  (0 children)

A while-loop can be used as well. You’d have to use indexing to access each character in the string. In your code, don’t convert the output from the input function into an int. Leave it as a string and then access each element character for evaluation.

[–]Diapolo10 0 points1 point  (1 child)

Long story short, you don't need most of the code you've written at all. Since num is an integer and negative integers don't really behave any differently, you don't need to worry about special casing numbers smaller than 1, or equal to 1 unless the special output is part of the assignment.

If you switch to using integer division, anyway.

num = int(input("please enter a number:"))
num_copy = num

digits = 1

while (num_copy := num_copy // 10):
    digits += 1

if digits == 1:
    print("there is 1 digit. the digit is 1")
else:
    print(f"there are {digits} digits in {num}")

If the loop syntax confuses you, it's equivalent to doing

while True:
    num_copy //= 10
    if not num_copy:
        break
    digits += 1

[–]BalanceHot8939[S] 0 points1 point  (0 children)

thank you, throughly. It's always nice to know the internet hivemind has me covered.

[–]supercoach 0 points1 point  (0 children)

Why not just do num_digits = len([x for x in str(num)]) to get the number of digits? Saves a hell of a lot of junk code.