Home > Mobile >  i have thes exam and i dont know what is the problem in my answer
i have thes exam and i dont know what is the problem in my answer

Time:11-25

Write a Python code snippet use 'if-elif' flow control along with a 'while' loop that will:

  1. Instruct a user to input a number that is greater than 0 and less than or equal to 10 and store the input as a floating-point value in a variable
  2. If the input number is greater than 0 and less than or equal to 10,
  3. use a 'while' loop in order to add the number to itself until the sum exceeds a value of 100.
  4. After the sum has exceeded a value of 100, use the print statement to output the sum
  5. Otherwise, output the message 'You did not enter a value between 0 and 10'

My Answer :

inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
    while inval < 100:
        inval  = inval
        continue
    else:
        print(inval)
elif inval <= 0 or inval > 10:
    print('You did not enter a value between 0 and 10')

CodePudding user response:

The reason your answer is not acceptable is because the code should return 100.0 for input like 6.25. But, your code returns 200.0 because of your while condition. You can just add an equal sign(=) to solve this problem. And also, I removed unnecessary parts from your code.

inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
    while inval <= 100:
        inval  = inval
    print(inval)
else:
    print('You did not enter a value between 0 and 10')

CodePudding user response:

I think that the only thing that may be wrong is the condition of the While loop (u should put it inval <= 100). However, the problem could be on the else inside the loop; because, its not "efficient", after the inval value is greater than 100 it will print the value even if the else is not there. See:

inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
    while inval <= 100:
        inval  = inval
    print(inval)

elif inval <= 0 or inval > 10:
    print('You did not enter a value between 0 and 10')

I don't see other problem in your code :)

  • Related