113k views
5 votes
Write a Python program (or function) to prompt the user for a beginning and ending number. The program must check for the following: - Assume only positive numbers - The ending number must be higher than the beginning number - Both entries must be integers If any of these requirements are not met, then the following error message should be displayed to the user (If more than one violation is present, you only need to display the error message for one of the violations): - For non-positive integers: Only positive integers are allowed. Either "> " or "> " is not positive. - For non-integers: Only positive integers are accepted, "" or "" does not seem to be an integer. - If second number is bigger than the first one: The second number "" cannot be smaller than or equal to the first number "" If the user supplied entries do not violate any of the rules, then display the results and the process to the user. For example, if someone enters 3 and 6 as their first and second number respectively, you should display the following:

++3+4=7
++7+5=12
++12+6=18

Your result is 18. sum_num() Please enter the smaller number: > ? 5 Please enter the bigger number: > ? A 'Only positive integers are accepted, "5" or "A" does not seem to be an integer. 2. When the second number is smaller than the first: Pys sum_num() Please enter the smaller number: > ? 5 Please enter the bigger number: > ? 2 'The second number " 2 " cannot be smaller than or equal to the first number " 5 ". 3. When at least one of the supplied numbers is negative or 0 : P>s sum_num() Please enter the smaller number: > ? -2 Please enter the bigger number: > ? 5 'Only positive integers are allowed. Either "5" or "-2" is not positive.' 4. When all conditions are met: Py sum_num() Please enter the smaller number: > ? 13 Please enter the bigger number: > ? 17 ++13+14=27 ++27+15=42 ++42+16=58 ++58+17=75 'Your result is 75. '

User Lcarsos
by
7.9k points

1 Answer

4 votes

Answer:

def sum_num():

smaller_number = input("Please enter the smaller number: ")

bigger_number = input("Please enter the bigger number: ")

# Check if both entries are integers

if not smaller_number.isdigit() or not bigger_number.isdigit():

print("Only positive integers are accepted.")

print(f'"{smaller_number}" or "{bigger_number}" does not seem to be an integer.')

return

smaller_number = int(smaller_number)

bigger_number = int(bigger_number)

# Check if both entries are positive

if smaller_number <= 0 or bigger_number <= 0:

print("Only positive integers are allowed.")

if smaller_number <= 0:

print(f'Either "{smaller_number}" is not positive.')

else:

print(f'Either "{bigger_number}" is not positive.')

return

# Check if the second number is bigger than the first number

if smaller_number >= bigger_number:

print(f'The second number "{bigger_number}" cannot be smaller than or equal to the first number "{smaller_number}".')

return

# Calculate the sum and display the process

result = smaller_number

process = ""

while smaller_number < bigger_number:

process += f'++{smaller_number}+{smaller_number+1}={result+smaller_number+1}\\'

result += smaller_number+1

smaller_number += 1

print(process)

print(f"Your result is {result}.")

sum_num()

User Lumbric
by
7.5k points

No related questions found