第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使用整數(shù)迭代列表中索引的值

如何使用整數(shù)迭代列表中索引的值

慕哥6287543 2021-06-10 18:58:06
class Planet:    def __init__(self, name, radius, system, gravity):        self.name=name        self.radius=radius        self.gravity=gravity        self.system=system    def orbit(self):        return(f'{self.name} is orbiting in the {name} system')planet_list=[]while True:    name = input('Enter the Planet\'s name: \n')    radius = int(input('Enter the Planet\'s radius: \n'))    gravity = int(input('Enter the Planet\'s gravity: \n'))    system = input('Enter the Planet\'s system: \n')    sub_list=[name,radius,gravity,system]    planet_list.append(sub_list)    again=input('\nAdd another planet? (Y/N): ')    if again.startswith('y') or again.startswith('Y'):        continue    elif again.startswith('n') or again.startswith('N'):        break    else:        print('I didn\'t quite catch that...')        continuedef planet_intro(dictionary):     for world in planet_list:        i=0        print(f'\nPlanet: {planet_list[i][0]}\nRadius: {planet_list[i][1]}\nGravity: {planet_list[i][2]}\nSystem: {planet_list[i][3]}\n')        i+=1planet_intro(planet_list)我在這方面已經(jīng)有一段時(shí)間了,但無(wú)論出于何種原因,我都無(wú)法聲明一個(gè)整數(shù)并將該變量用作索引的值。我今天剛開(kāi)始使用 Java,所以這可能是一個(gè)非常簡(jiǎn)單的修復(fù)方法,但我還沒(méi)有弄清楚。因此,如果我運(yùn)行它,它會(huì)為 中的多個(gè)列表重復(fù)相同的數(shù)據(jù)planet_list,而不迭代索引的值(更改打印的數(shù)據(jù))。
查看完整描述

2 回答

?
慕俠2389804

TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊

你的迭代有點(diǎn)不正確。


用:


def planet_intro(planet_list):

     for world in planet_list:

        print(f'\nPlanet: {world[0]}\nRadius: {world[1]}\nGravity: {world[2]}\nSystem: {world[3]}\n')


查看完整回答
反對(duì) 回復(fù) 2021-06-29
?
飲歌長(zhǎng)嘯

TA貢獻(xiàn)1951條經(jīng)驗(yàn) 獲得超3個(gè)贊

您的打印語(yǔ)句相同的原因是您需要采取i=0循環(huán)的外部(因?yàn)槊看窝h(huán)時(shí),您都將重新設(shè)置i為0)。此外,您應(yīng)該將其作為輸入planet_list


def planet_intro(planet_list):

     i=0

     for world in planet_list:

        print(f'\nPlanet: {planet_list[i][0]}\nRadius: {planet_list[i][1]}\nGravity: {planet_list[i][2]}\nSystem: {planet_list[i][3]}\n')

        i+=1

您還可以使用 enumerate 迭代列表的項(xiàng)目并擁有索引。您的代碼希望:


def planet_intro(planet_list):

     for i, world in enumerate(planet_list):

        print(f'\nPlanet: {planet_list[i][0]}\nRadius: {planet_list[i][1]}\nGravity: {planet_list[i][2]}\nSystem: {planet_list[i][3]}\n')

但我仍然認(rèn)為 Rakesh 擁有最好的代碼,所以我會(huì)選擇他的。


查看完整回答
反對(duì) 回復(fù) 2021-06-29
  • 2 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)