'Writing a program to divide user_num by x 3 times

I am trying to write a program for my class that divides the user_num by x three times. Thus far, I have:

user_num = input()
x = input()

So what is the missing link to do this? By three times, it should be like if the user inputs:

2000
2

Then the output should be:

1000 500 250

Am I on the right track and if so, what is the next step to make this work?



Solution 1:[1]

While StackOverflow is not a code writing service, this problem is hard to explain without simply giving it away.

The first step is simply what you have, except the numbers are also converted into a numeric type to allow for mathematical operations.

user_num = int(input("enter a number: "))
x = int(input("enter x: "))

After that, you want to divide user_num by x, and then print the result, three times over. This is done using a loop. // will perform the division and ignore the decimal. print will work as expected, and the end argument will format the output as you described.

for _ in range(3):
  user_num = user_num // x
  print(user_num, end=" ")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cresht