當我運行下面的代碼時,我經常受到用戶輸入之間打印的對象創建確認的負載的影響......如果有某種方式可以在用戶專注於輸入答案的同時使控制檯保持沉默。我找到了一些粗略的解決方案,但它們看起來太過複雜,因爲問題的簡單程度如何。下面的代碼片段和示例輸出。謝謝!Python:關閉控制檯/ shell中的自動輸出,讓用戶輸入時詢問用戶輸入
代碼:
#Engages in battles until user quits
def battle(self):
battles_threshold = min([a.battles_fought for a in self.artists])
contenders = [a for a in self.artists if
a.battles_fought == battles_threshold]
noncontenders = list(set(self.artists)-set(contenders))
print(noncontenders)
contenders.append(random.choice(contenders))
contender1=contenders.pop(random.choice(range(len(contenders))))
contender2=contenders.pop(random.choice(range(len(contenders))))
elo1 = contender1.elo
elo2 = contender2.elo
outcome = ''
while (outcome != 'y' and outcome != 'n' and
outcome != 'm' and outcome != 'q'):
outcome = \
raw_input('Do you like "' + contender1.name +
'" more than "' + contender2.name + '"?\n')
if outcome == 'q':
return None
elif outcome == 'y':
contender1.elo_calc(1,contender2.name,elo2)
contender2.elo_calc(0,contender1.name,elo1)
elif outcome == 'n':
contender1.elo_calc(0,contender2.name,elo2)
contender2.elo_calc(1,contender1.name,elo1)
else:
contender1.elo_calc(0.5,contender2.name,elo2)
contender2.elo_calc(0.5,contender1.name,elo1)
pickle.dump(music_battler, open(self.path_name +
'music_battler.p','wb'))
self.battle()
輸出:
Do you like "basshunter" more than "lil wayne"?
y
[<__main__.Artist instance at 0x10dc91ab8>, <__main__.Artist instance at 0x10dc7b050>, <__main__.Artist instance at 0x10dcdb878>, <__main__.Artist instance at 0x10dc82098>, <__main__.Artist instance at 0x10dc830e0>, <__main__.Artist instance at 0x10dc7b908>, <__main__.Artist instance at 0x10da15d88>, <__main__.Artist instance at 0x10dcd1128>, <__main__.Artist instance at 0x10dc7b950>, <__main__.Artist instance at 0x10da0c170>, <__main__.Artist instance at 0x10dccf998>, <__main__.Artist instance at 0x10dc911b8>, <__main__.Artist instance at 0x10dcd89e0>, <__main__.Artist instance at 0x10da1f050>, <__main__.Artist instance at 0x10da0d200>, <__main__.Artist instance at 0x10dc8b1b8>, <__main__.Artist instance at 0x10dab4c68>, <__main__.Artist instance at 0x10dcd1a70>, <__main__.Artist instance at 0x10dcd8290>, <__main__.Artist instance at 0x10dc8b878>, <__main__.Artist instance at 0x10dc9f320>, <__main__.Artist instance at 0x10dcd1dd0>, <__main__.Artist instance at 0x10dc91b00>, <__main__.Artist instance at 0x10dc7b488>, <__main__.Artist instance at 0x10da0d320>, <__main__.Artist instance at 0x10da22b48>, <__main__.Artist instance at 0x10dc85098>, <__main__.Artist instance at 0x10da0bb48>, <__main__.Artist instance at 0x10dcd1c20>, <__main__.Artist instance at 0x10dc7bcb0>, <__main__.Artist instance at 0x10dc85a70>, <__main__.Artist instance at 0x10da1dc68>, <__main__.Artist instance at 0x10dc85e18>, <__main__.Artist instance at 0x10daee488>, <__main__.Artist instance at 0x10dc9fcb0>, <__main__.Artist instance at 0x10dcd14d0>, <__main__.Artist instance at 0x10dc91cf8>, <__main__.Artist instance at 0x10da39d88>, <__main__.Artist instance at 0x10dccfdd0>, <__main__.Artist instance at 0x10dc915f0>, <__main__.Artist instance at 0x10dc91908>, <__main__.Artist instance at 0x10dcddea8>, <__main__.Artist instance at 0x10da2aa70>, <__main__.Artist instance at 0x10dc916c8>, <__main__.Artist instance at 0x10dc85ef0>, <__main__.Artist instance at 0x10dccf710>, <__main__.Artist instance at 0x10dc85758>, <__main__.Artist instance at 0x10da11f80>, <__main__.Artist instance at 0x10dc7bfc8>, <__main__.Artist instance at 0x10dc9f7e8>]
Do you like "alesso" more than "taking back sunday"?
也沒有這樣的東西,自動輸出:) – Untitled123