1

Employ a Python program to ask the user to enter the mean and variance of the distribution. Tell the user that the mean can be any value between minus infinity (–∞) and plus infinity (+∞), but the variance must be a value larger than 0. Integrate certain control mechanism to ensure that the variance condition is fulfilled, and the input is numeric. If the user press ENTER without providing any values, the program will automatically set μ to 0 and σ2 to 1.

while True: 

    mean = int(input("please enter the mean of the distribution (any value between –∞ and +∞) "))
    variance = int(input("please enter the variance of the distribution (must be larger than 0) "))

    if variance >= 0:
        mean = (float(mean))
        variance = (float(variance))

        print(mean)
        print(variance)


    elif variance == int(""):
        mean = 0.0
        variance = 1.0

        print(variance)

    else:
        print("Variance input is smaller than zero")
        break
kuro
  • 3,214
  • 3
  • 15
  • 31
  • What is the problem you have? – 9769953 Aug 04 '21 at 11:27
  • 1
    You may want to write two separate `while` loops, one for the `mean` input and one for the `variance` input. Design each loop so that it is only exited if the input fulfills the conditions. – Arne Aug 04 '21 at 11:32
  • I am unable to get the program running if the user press ENTER without providing values, the program will automatically set μ to 0 and σ2 to 1. – Coder20192020 Aug 04 '21 at 11:33
  • 3
    Writing int(input('Prompt')) is a bad idea because if the input cannot be interpreted as an integer, you'll get a ValueError exception. Consider using try/except. int("") will induce ValueError –  Aug 04 '21 at 11:39
  • As Andy says, don't convert the input to integer immediately. If you keep the input as a string at first, you can check for an empty input and adjust the value accordingly, e.g.: `if mean == '': mean = 0` – Arne Aug 04 '21 at 12:12

0 Answers0