我正在编写一个计算1-50之间奇数和偶数之和的程序。
到目前为止,这就是我所拥有的:
显示消息
print("Sums of odd and and even numbers")
print("="*45)
询问用户是否想继续。
ch = 'y'
while(ch == 'y'):
# Prompt the user to enter a number.
# Store the number entered by the user in the variable n.
num =int(input('Enter a whole number between 1 and 50: '))
# If the number entered by the user is in the range 1-50
while(num<1 or num>50):
num = int(input("Try again. Your number must be between 1 and 50: "))
# Check if the valid number entered by the user is even or odd.
# If number mod 2 == 0, then the number is even
if num%2 == 0:
# Print the message.
print('')
print('Your number is an even number. ')
# Initialize the variable even_sum to 0.
# This variable is used to store sum of even numbers
even_sum=0
# for loop to find sum of even numbers
for i in range(2,num+1,2):
even_sum+=i
# Print the sum of the even numbers from 2 till the number
# entered by the user.
print('The sum of the even numbers from 2 to %d is : '%num,even_sum)
else:
# If number mod 2 is not equal to 0, print the message.
# So, the number is odd.
print('')
print('Your number is an odd number.')
# Initialize the variable odd_sum to 0.
# This variable is used to store sum of odd numbers
odd_sum=0
# for loop to find sum of odd numbers
for i in range(1,num+1,2):
odd_sum+=i
# Print the sum of the odd numbers from 1 till the number
# entered by the user.
print('The sum of odd numbers from 1 to %d is: '%num,odd_sum)
print('')
# Ask the user whether he/she wants to continue the program.
ch = input("\n Try again? (y/n) ")
这个程序可以工作,但是我只需要添加一个循环来检查num是不是和整数。就像num是浮点数或字符串一样,然后再次提示输入介于1到50之间的整数。
发布于 2022-02-23 14:00:46
你可以这样做:
ch = 'y'
while(ch == 'y'):
num = 2
# Prompt the user to enter a number.
# Store the number entered by the user in the variable n.
while True:
if isinstance(num, int):
if num<1 or num>50:
num = input('Try again. Your number must be between 1-50: ')
else:
num = input('Enter a whole number between 1 and 50: ')
else:
num = input('Enter a whole number between 1 and 50: ')
try:
num = int(num)
if num<1 or num>50:
continue
break
except ValueError:
print("You did not enter a number, try again.")
# Check if the valid number entered by the user is even or odd.
# If number mod 2 == 0, then the number is even
if num%2 == 0:
# Print the message.
print('')
print('Your number is an even number. ')
# Initialize the variable even_sum to 0.
# This variable is used to store sum of even numbers
even_sum=0
# for loop to find sum of even numbers
for i in range(2,num+1,2):
even_sum+=i
# Print the sum of the even numbers from 2 till the number
# entered by the user.
print('The sum of the even numbers from 2 to %d is : '%num,even_sum)
else:
# If number mod 2 is not equal to 0, print the message.
# So, the number is odd.
print('')
print('Your number is an odd number.')
# Initialize the variable odd_sum to 0.
# This variable is used to store sum of odd numbers
odd_sum=0
# for loop to find sum of odd numbers
for i in range(1,num+1,2):
odd_sum+=i
# Print the sum of the odd numbers from 1 till the number
# entered by the user.
print('The sum of odd numbers from 1 to %d is: '%num,odd_sum)
print('')
# Ask the user whether he/she wants to continue the program.
ch = input("\n Try again? (y/n) ")
测试:
Enter a whole number between 1 and 50: g
You did not enter a number, try again.
Enter a whole number between 1 and 50: gew
You did not enter a number, try again.
Enter a whole number between 1 and 50: 10.0
You did not enter a number, try again.
Enter a whole number between 1 and 50: 1
Your number is an odd number.
The sum of odd numbers from 1 to 1 is: 1
Try again? (y/n) y
Enter a whole number between 1 and 50: 10
Your number is an even number.
The sum of the even numbers from 2 to 10 is : 30
Try again? (y/n) n
https://stackoverflow.com/questions/71238064
复制相似问题