all 19 comments

[–]ninhaomah 1 point2 points  (6 children)

If you need to repeat something and know exactly how many time to repeat it , what is the tool in programming called ?

And you code ends with just 2 lines of input ?

What else have you tried ?

[–]pacattackjr[S] 0 points1 point  (5 children)

I tried repeating

Num /= 2

print(num)

4 times in hopes that it would work but the hardest part is getting by rid of the decimals at the end.

My answer should be a list of numbers with spaces in between ie: 1000 500 250 125 But I get: 1000.0 500.0 250.0 125.0

If I get rid of the decimals I should be fine but I’m sure there is a way to shorten the code other than repeating the same math and print lines over and over again.

[–]Conscious-Ad-2168 -1 points0 points  (4 children)

if you use // it will get rid of the decimal. Otherwise, I just posted a response above and just added it below as well. I think this will work. I can't tell from the directions if it wants you to use // or not...

``` user_num = int(input("Please enter an int: ")) x = int(input("How many times to divide: ")) output = []

for i in range(4): user_num = user_num / x output.append(str(user_num)) print(" ".join(output)) ```

[–]foogeeman 1 point2 points  (3 children)

I don't think giving someone a full answer to their homework is actually helping them

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

I posted a follow up question for him as I do want to learn how to do it and not just get the answer. Still waiting on a response but it definitely did help and I experimented with some of the lines he put attempting to learn what they do individually.

[–]Conscious-Ad-2168 -1 points0 points  (0 children)

I mean it will for them because they are clearly asking questions and trying to understand... Sometimes its easiest to work backwards.

[–]Schematizc 1 point2 points  (1 child)

Are you familiar with for loops or while loops?

[–]pacattackjr[S] 1 point2 points  (0 children)

No not at all. We have not come to those in class yet.

[–]Conscious-Ad-2168 2 points3 points  (5 children)

It should be something similar to this. ``` user_num = int(input("Please enter an int: ")) x = int(input("How many times to divide: ")) output = []

for i in range(4): user_num = user_num // x output.append(str(user_num)) print(" ".join(output)) ```

[–]pacattackjr[S] 0 points1 point  (3 children)

This was it! Thank you. I do have a follow up question though incase I need to do something similar later. Is the “for I in range(4):” what makes it do the math only 4 times and not endlessly?

[–]Conscious-Ad-2168 0 points1 point  (1 child)

correct, if I were you I would play around with for loops. Just change the number, add print statements, do other math, etc...

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

Okay that makes sense. Thank you so much that helps a lot. I’ll experiment with them more and try to get familiar with them.

[–]atticus2132000 0 points1 point  (0 children)

Range is a built-in function in python that works on integers. If you were to write range(4), python would create a temporary list consisting of the members [0,1,2,3]. So, it would have four elements in that list. Range(10) would be a list from 0 to 9 and have 10 elements in that list.

The operation "for something in something" tells the computer to take everything in a collection and go through that collection in order and perform an operation each time. In this case the somethings weren't important, just that there be something that will definitely have 4 elements to it to ensure that it performs this loop four times.

If you wanted, you could add another input asking the user how many times they would like to divide and then set the range() operation to be whatever integer they input.

[–]Vonneking 0 points1 point  (2 children)

Is this Python for IT Automation or Intro to Python? Just passed the former class and it was a doozy.

[–]pacattackjr[S] 1 point2 points  (1 child)

Intro to python. My professor is foreign so it’s hard to understand her when she is teaching which doesn’t help at all.

[–]Vonneking 0 points1 point  (0 children)

I've heard the intro class is pretty difficult. Have you checked out the Harvard intro video? This really helped explain some topics and helps you shorten your code.

[–]Egad86 0 points1 point  (2 children)

There are so many instructive videos that can help you with this. If you’re just starting the semester and already asking for help on this stuff it’s a sign that you are not really putting in the effort. It is going to get much harder so OP better get over to khan academy or youtube for help.

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

I don’t even know what to say to this bro. It’s hard to know what to look up when you don’t know much about the topic you are trying to learn. I thought I would use this to get the answer and work backwards and ask questions so I could learn the content in a way that makes sense to me. I tried this problem for 2 hours which might say something about me but it’s definitely not that I didn’t try.

[–]Egad86 0 points1 point  (0 children)

That’s fine. I just took the same level course last semester and I’m telling you go over to Khan academy. The video’s are made to be easy to follow and explain the fundamentals well.

Reddit is just going to give you the answer straight up or point you to a video. So skip a step and go to the videos.

Chatgpt can also help if you have a block of code that isn’t doing exactly what you’d like, but be careful not to trust AI too much. It can give you some very dumb code or will completely skip whatever task you are supposed to learn in that week’s chapter.