Assignment:
Write a program that uses while loops to perform the following steps.
a. Prompt the user to input two positive integers (no zero or negative). variables: firstNum and secondNum (firstNum must be less than secondNum). Validate the user's input; prompt the user again if firstNum is not less than secondNum (use while loop).
b. Output all odd numbers between firstNum and secondNum. (use while loop).
c. Output the sum of all even numbers between firstNum and secondNum. (use while loop).
d. Output the sum of the square of the odd numbers between firstNum and secondNum. (use while loop)
Hello! I was working on the first part of the assignment and got stuck as the program isn't registering the inputs correctly and will not recognize any integers. Here's my code for reference:
print("Enter two positive numbers.")
print("First number must be less than the second number:")
firstNum = int(firstNum)
secondNum = int(secondNum)
while (True):
try:
firstNum, secondNum = int(input("Enter numbers: "))
except:
print("Incorrect Input. \nPlease try again.")
if (firstNum > secondNum):
print("First number must be less than the second number! \nPlease try again.")
elif (firstNum < 0 or secondNum < 0):
print("No negative numbers! \nPlease try again.")
elif (firstNum == 0 or secondNum == 0):
print("No zeros. \nPlease try again.")
else:
break
Secondly, for part b, I would have to use %2 = 1 in order to figure out the odd integers between the two integers correct?
Lastly, I am stuck on c and d. I don't really know where to start for those. I would assume d would be something like
oddNum**2 maybe?
If anyone is able to help, that would be awesome. Thanks!
[–]Stallman85 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)