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

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

列表中的 Python 用戶輸入

列表中的 Python 用戶輸入

猛跑小豬 2021-12-09 15:48:20
我是 python 編程的新手,在任何地方都找不到這個(gè)幫助我有一個(gè)要在指定列表 exp 中搜索的用戶輸入值:option=input("option: ")iplist=['192.168.1.1', '192.168.1.2', '192.168.1.254']while option <= "3":  #this is wrong. Help!  nub = iplist[option]  subprocess.call(["ping", nub])我希望用戶的選項(xiàng)是該程序列表中的數(shù)字,輸出應(yīng)該是:Option : 0Pinging 192.168.1.1 with 32 bytes of data:Reply from 192.168.1.1: bytes=32 time=2ms TTL=64Reply from 192.168.1.1: bytes=32 time=2ms TTL=64Reply from 192.168.1.1: bytes=32 time=2ms TTL=64Reply from 192.168.1.1: bytes=32 time=2ms TTL=64Ping statistics for 192.168.1.1:    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),Approximate round trip times in milli-seconds:    Minimum = 2ms, Maximum = 2ms, Average = 2ms
查看完整描述

1 回答

?
隔江千里

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

為什么需要循環(huán)?使用in檢查,如果在列表中存在的輸入值,并相應(yīng)如下:


option=input("option: ")

iplist=['192.168.1.1', '192.168.1.2', '192.168.1.254']


if option in iplist:

   # do the rest

    pass

或:


如果要獲取Index列表中元素的 :


for index, elem in enumerate(iplist):

    if option == elem:

        print("Element found at Index: {}".format(index))

輸出:


option: 192.168.1.2

Element found at Index: 1

編輯 2:


首先要注意以下幾點(diǎn):


從用戶那里獲取輸入并將其轉(zhuǎn)換為 anint因?yàn)槟鸁o法使用 str 索引訪問列表:


我仍然沒有看到循環(huán)點(diǎn)


所以:


import subprocess

option= int(input("option: "))    # 1

iplist=['192.168.1.1', '192.168.1.2', '192.168.1.254']


nub = iplist[option]

subprocess.call(["ping", nub])

輸出:


Pinging 192.168.1.2 with 32 bytes of data:

Request timed out.

Request timed out.

Request timed out.

Request timed out.


Ping statistics for 192.168.1.2:

    Packets: Sent = 4, Received = 0, Lost = 4 (100% loss),


查看完整回答
反對(duì) 回復(fù) 2021-12-09
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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