當我編碼時,我從列表中選擇一個隨機值並將其與兩個數字並列打印以得出總和。但是,列表中的值仍然顯示引號,我不明白爲什麼。該代碼是:列表中的運算符在打印時仍顯示引號(Python 3.1)
import random
level = input('Please choose either easy, medium or hard')
if level == 'easy':
num1 = random.randint(1,5)
num2 = random.randint(1,5)
#Chooses a random operator from the list
op = random.choice(['+', '-', '*'])
#Arranges it so the larger number is printed first
if num1 > num2:
sum1 = (num1, op, num2)
else:
sum1 = (num2, op, num1)
#Prints the two numbers and the random operator
print(sum1)
我嘗試運行這段代碼,結果我給出的是:
(4, '*', 3)
當我希望它顯示爲:
4*3
這些數字是隨機也生成但工作正常。有誰知道如何解決這個問題?
我嘗試添加這並給出了以下錯誤消息:類型錯誤:序列項0:預期str實例,int發現 –
@DaisyBradbury我想我剛剛發現了這個問題。請參閱我的編輯。 – Mureinik
我無法使用此方法,因爲我需要將值保留爲整數,因爲我將其用於稍後在程序中進行計算。 –