亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

循環印刷的整個范圍

循環印刷的整個范圍

qq_遁去的一_1 2021-03-29 21:11:01
我是Python的新手,學習了For Loops并編寫了以下代碼:cars = [1, 2, 3, 4]trucks = ['red', 'blue', 'green', 'white']for numbers in cars:    print (f"I can count: {numbers}")for colors in trucks:    print(f"I can list colors: {colors}")new_list = []for numbers in range(0, 4):    new_list.append(numbers)for numbers in new_list:    print(f"I can count more: {new_list}!")我得到這個結果:I can count: 1I can count: 2I can count: 3I can count: 4I can list colors: redI can list colors: blueI can list colors: greenI can list colors: whiteI can count more: [0, 1, 2, 3]!I can count more: [0, 1, 2, 3]!I can count more: [0, 1, 2, 3]!I can count more: [0, 1, 2, 3]!對于“我可以數更多”的部分,如何獲取它以數字順序列出范圍:I can count more: 1! I can count more: 2!, etc.而不是打印行中的整個范圍?
查看完整描述

2 回答

?
慕妹3146593

TA貢獻1820條經驗 獲得超9個贊

print(f"I can count more: {new_list}!")

應該

print(f"I can count more: {numbers}!")

因為new_list是整個列表,并且numbers是您for在中定義的變量for numbers in new_list


查看完整回答
反對 回復 2021-04-09
?
人到中年有點甜

TA貢獻1895條經驗 獲得超7個贊

使用“數字”代替“ new_list”。并且不要在同一列表上一次又一次地迭代,因為new_list包含您在上一個循環中顯示的相同項目。所以,您的代碼應該是這樣的,


for numbers in range(0, 4):

    print(f"I can count more: {numbers}!")


查看完整回答
反對 回復 2021-04-09
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號