Reputation: 21
I;m writing code, this code should prompts the user for 3 positive numbers (or zero) and then adds them together. If the user enters a negative number, the program should reprompt them until they enter a positive number. But it is not working, when I add negative numbers, the code took it as a part of the final result, and it is supposed that just take the positive. Any help or advice will be more than welcome.
def prompt_number(notPositive):
print("Invalid entry. The number must be positive.")
def compute_sum(a, b, c):
return a + b + c
def main():
while True:
num1 = int(input("Enter a positive number: "))
if num1 >= 0:
print()
else:
print(prompt_number(num1))
num2 = int(input("Enter a positive number: "))
if num2 >= 0:
print()
else:
print(prompt_number(num2))
num3 = int(input("Enter a positive number: "))
if num3 >= 0:
print()
else:
print(prompt_number(num3))
break
result = compute_sum(num1, num2, num3)
print (str(result))
main()
Upvotes: 1
Views: 1724
Reputation: 2702
def prompt_number(notPositive):
print("Invalid entry. The number must be positive.")
There's probably no need to make a function for this. Not only is the notPositive
not following style conventions (it should be not_positive
), it isn't even used by the function.
def compute_sum(a, b, c):
return a + b + c
There's really not much point in having a function just for this, either.
Onto the issue with the main part of the code: when I add negative numbers, the code took it as a part of the final result There's a rather simple reason for that, it's because you never told the program not to. Try walking through the execution of the code on paper.
Clean and straightforward!
def get_pos_num():
while True:
curr_val = int(input("Enter a positive integer: "))
if curr_val >= 0:
return curr_val
else:
input("Invalid input: Not a positive number.\n")
while True:
print("\nProgram to calculate the sum of 3 positive integers.")
first_num = get_pos_num()
second_num = get_pos_num()
third_num = get_pos_num()
num_sum = first_num + second_num + third_num
print(f"You entered the following numbers: {first_num}, {second_num}, {third_num}.")
print(f"Their sum is: {num_sum}\n")
Let me know if you have any questions :)
Upvotes: 1
Reputation: 14886
It is because you skip when user input is negative, you should keep looping until the desired input is fed.
def prompt_number(notPositive):
print("Invalid entry. The number must be positive.")
def compute_sum(a, b, c):
return a + b + c
def main():
num1 = -1
num2 = -1
num3 = -1
while num1 < 0:
num1 = int(input("Enter a positive number: "))
if num1 >= 0:
break
else:
print(prompt_number(num1))
while num2 < 0:
num2 = int(input("Enter a positive number: "))
if num2 >= 0:
break
else:
print(prompt_number(num2))
while num3 < 0:
num3 = int(input("Enter a positive number: "))
if num3 >= 0:
break
else:
print(prompt_number(num3))
result = compute_sum(num1, num2, num3)
print (str(result))
main()
A better solution is to write a get_positive_number method to eliminate code duplication.
def get_positive_number():
num = -1
while num < 0:
num = int(input())
if num >= 0:
return num
else:
prompt_number()
def prompt_number():
print("The number must be positive.")
def compute_sum(a, b, c):
return a + b + c
def main():
num1 = get_positive_number()
num2 = get_positive_number()
num3 = get_positive_number()
result = compute_sum(num1, num2, num3)
print (str(result))
main()
Upvotes: 2