📅  最后修改于: 2023-12-03 15:13:10.071000             🧑  作者: Mango
本解决方案是针对印度国家课程体系中第八类NCERT教材第16章“玩数字游戏”的练习16.1而编写的。通过此程序,学生可以练习比较两个数字的大小,并根据其大小进行相应的操作。该程序使用Python编写,并通过简单的控制台界面与用户交互。
主要功能包括:
python game.py
比较输入的数字,并执行相应的操作:
Enter first number: 5
Enter second number: 3
The sum of 5 and 3 is 8
两个数字相等:
Enter first number: 4
Enter second number: 4
The numbers are equal.
第二个数字比第一个数字大:
Enter first number: 7
Enter second number: 9
The product of 7 and 9 is 63
# Game to compare two numbers and perform an operation based on the comparison
# Accept two numbers as input
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
# Compare the two numbers
if num1 > num2:
# Calculate the sum of the two numbers if the first number is greater
result = num1 + num2
print("The sum of", num1, "and", num2, "is", result)
elif num2 > num1:
# Calculate the product of the two numbers if the second number is greater
result = num1 * num2
print("The product of", num1, "and", num2, "is", result)
else:
# If the two numbers are equal, output that they are equal
print("The numbers are equal.")
该代码片段将接受两个数字作为输入,比较它们的大小,并根据比较结果执行相应的操作:计算它们的和(如果第一个数字大于第二个数字),计算它们的积(如果第二个数字大于第一个数字)或者提示用户两个数字相等。