我需要让输出看起来像这样:“(num1)和(num2)的总和是(num3)和(num1)>(num2)。”或反之亦然,哪个数字更大。我无法弄清楚如何在不获取错误代码的情况下将这些字符串附加或连接在一起。 +和\不能加入它们,我甚至尝试用它的新名称制作另一个字符串并将它们连接在一起。
CODE:
num1 = int(input("Enter number 1:"))
num2 = int(input("Enter number 2:"))
num3 = num1 + num2
print (" The sum of", (num1))
print ("and", (num2))
print ("is:", (num3))
the_text = "".join([ "The sum of", (num1), "and", (num2), "=" (num3)])
if num1 > num2:
print ("The number", (num1))
print (">")
print (num2)
elif num2 > num1:
print ("The numer", (num2))
print (">")
print (num1)
else:
print (num1)
print ("=")
print (num2)
input ("Press enter to close.")
答案 0 :(得分:0)
假设所需的语言是Python,
comparison_result = ""
if num1 > num2:
comparison_result = ">"
elif num1 < num2:
comparison_result = "<"
else:
comparison_result = "="
the_text = "The sum of " + str(num1) + " and " + str(num2) + " is " + str(num1 + num2) + " and " + str(num1) + comparison_result + str(num2)
您只需要将int
标记为str
。
答案 1 :(得分:0)
您可以使用格式:
cmp_char = ""
if a > b:
cmp_char = ">"
elif a < b:
cmp_char = "<"
else:
cmp_char = "="
print "The sum of {} and {} is {} and {} {} {}".format(num1, num2, num3, num1, cmp_char, num2)