簡體   English   中英

我怎樣才能使這個腳本更短?

[英]How can i make this script shorter?

這是我發現 speedtest-cli 時的第一個小型 python 任務。

import speedtest

q = 1
while q == 1:
    st = speedtest.Speedtest()
    option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
    if  option == 1:
        print(st.download())
        q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))

    elif option == 2:
        print(st.upload())
        q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))

    elif option == 3:
        servernames =[]
        st.get_servers(servernames)
        print(st.results.ping)
        q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))

    else:
        print("Please enter the correct choice")
else:
    print("Test is ended")

我只是一個初學者,所以我找不到任何方法來縮短這段代碼。 任何提示都會有所幫助:)

如果您不關心執行時間而只關心代碼長度:


import speedtest

q = 1
while q == 1:
    st = speedtest.Speedtest()
    st.get_servers([])
    tst_results = [st.download(), st.upload(), st.results.ping]
    option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
    if option >= 1 and option <= 3:
        print(tst_results[option-1])
        q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))
    else: 
        print("Please enter the correct choice")
else:
    print("Test is ended")

並沒有真正讓它變得更聰明,只是通過創建一個列表並將選項作為列表中的索引來縮短

首先,你可以看看這一行:

q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))

這條線出現在每個選項上,所以我們不需要重復它。 相反,您可以將其添加到“while”子句的末尾。 還要在“else”子句中添加“continue”,以避免在輸入錯誤輸入時詢問這個問題。

此外,不需要“while”循環的“else”子句

例如:

import speedtest

q = 1
while q == 1:
    st = speedtest.Speedtest()
    option = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))
    if  option == 1:
        print(st.download())

    elif option == 2:
        print(st.upload())

    elif option == 3:
        servernames =[]
        st.get_servers(servernames)
        print(st.results.ping)

    else:
        print("Please enter the correct choice")
        continue

    q = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))

print("Test is ended")

第二,你的邏輯有錯誤。 您的提示說輸入 1 繼續或 2 退出,實際上當您輸入 2 時循環將結束,但當用戶輸入 3 或任何其他數字時也是如此。 更糟糕的是,如果用戶輸入的字符不是數字或根本不是數字,他們將得到異常。 為此,我們使用 try-except 子句。 執行這種循環的另一種方法是使用“while”True,然后使用“break”退出。

while True:
    ... your code here ...

    q = input("enter 1 or 2:")
    try:
        q = int(q)
    except ValueError:
        print("Invalid input")

    if q == 2:
        break

print("Test is ended")

如果出現以下情況,這對您很有幫助:

  • 你是個滲透測試者
  • 或者您(出於其他任意原因)正在尋找一種方法來擁有一個非常小的 python 腳本

免責聲明:我不推薦這個,只是說它可能會有所幫助。

腳步

  1. 將所有\n (換行符)替換為\\n
  2. \\t替換兩個(或四個)空格(取決於您的喜好)
  3. 在它周圍加上""" (pythons長引號)
  4. 將該單行字符串放入exec() function (不推薦,但如果您願意,可以這樣做)

應用於此問題代碼

exec("""import speedtest\n\nq = 1\nwhile q == 1:\n\t\tst = speedtest.Speedtest()\n\t\toption = int(input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: "))\n\t\tif\toption == 1:\n\t\t\t\tprint(st.download())\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telif option == 2:\n\t\t\t\tprint(st.upload())\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telif option == 3:\n\t\t\t\tservernames =[]\n\t\t\t\tst.get_servers(servernames)\n\t\t\t\tprint(st.results.ping)\n\t\t\t\tq = int(input("Enter '1' if you want to continue or Enter '2' if you want to stop the test"))\n\n\t\telse:\n\t\t\t\tprint("Please enter the correct choice")\nelse:\n\t\tprint("Test is ended")\n""")

我通過使用 sys 模塊找到了解決方案。 我想如果輸入錯誤的數據,這可能會起作用。

import speedtest
import sys
#Loop for options
while True:
    st = speedtest.Speedtest()
    option = input("What do you want to test:\n 1)Download Speed\n 2)Upload Speed \n 3)Ping \n Please enter the number here: ")
    #To check if the input value is an integer
    try:
        option = int(option)
    except ValueError:
        print("Invalid input")
        continue

    if  option == 1:
        print(st.download())

    elif option == 2:
        print(st.upload())

    elif option == 3:
        servernames =[]
        st.get_servers(servernames)
        print(st.results.ping)

    else:
        print("Please enter the correct choice: ")
        continue

    q = 0
    #Choice to continue or to end
    while (q != 1) or (q != 2):
        q = input("Enter '1' if you want to continue or Enter '2' if you want to stop the test: ")
        try:
            q = int(q)
        except ValueError:
            print("Invalid input")
            continue
        if q == 1:
            break
        elif q == 2:
            sys.exit("Test is ended")
        else:
            print("Invalid input")
            continue

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM