Write a while loop that prints user_num divided by 2 until user_num is less than 1. The value of user_num changes inside of the loop
in python please help! ​

Respuesta :

Answer:

  1. user_num = float(input("Enter a number: "))
  2. while(user_num > 1):
  3.    user_num = user_num / 2
  4.    print(user_num)

Explanation:

Line 1:

Use built-in function input() to prompt user input a number and assign it to the variable user_num.

Since the default data type of the user input is a string, we need to convert it into float data type before assigning it to user_num. We can do the data type conversion by enclosing the user input into the built-in function float().

Line 3:

Create a while loop by setting condition while user_num bigger than 1, the line 4 & 5 should keep running.

Line 4:

Divide user_num by 2 and assign the division result back to the user_num.

Line 5:

Display updated value of user_num using built-in function print()

fichoh

The required function in python is written as follows :

user_num = int(input())

#takes an input value and convert to an integer value.

while user_num >= 1 :

#While loops is conditioned to run if user_num is ≥ 1

user_ num = user_num / 2

#divide user_num by 2 and store the result as user_num

print(user_num)

#print user_num

The while loop condition does most of the work here, The while loops is conditioned to run the divison of user_num if the value of user_num is greater than or equal to 1 ; if user_num is lesser than 1, the loop terminates.

Lear more : https://brainly.com/question/15170131

ACCESS MORE