ShreddedPumpkin
ShreddedPumpkin

Reputation: 15

How can i make this script shorter?

This is my first small python task when i found out about speedtest-cli.

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")

i am just a beginner so i could't find any way to shorten this code. Any tips would be helpful :)

Upvotes: 0

Views: 126

Answers (4)

ShreddedPumpkin
ShreddedPumpkin

Reputation: 15

I have found the solution to that by using the sys module. i suppose this could work if a wrong data is input.

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

Upvotes: 0

publicdomain
publicdomain

Reputation: 1722

This is helpful to you if:

  • you are a pentester
  • or you (for some other arbitrary reason) are looking for a way to have a very small python script

Disclaimer: I do not recommend this, just saying it may be helpful.

Steps

  1. Replace all \n (newlines) by \\n
  2. Replace two (or four) spaces (depending on your preferences) by \\t
  3. Put """ (pythons long quotation marks) around it
  4. Put that oneline string into the exec() function (not recommended, but do it if you want to)

Applied to this questions code

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""")

Upvotes: 0

Almog-at-Nailo
Almog-at-Nailo

Reputation: 1182

First, you can look at this line:

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

this line happens on each of the options, so we don't need to repeat it. instead you can add it at the end of the "while" clause. also add "continue" to your "else" clause to avoid asking this when wrong input is entered.

also, the "else" clause for the "while" loop is not needed

e.g:

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")

second, there is an error in your logic. your prompt says enter 1 to continue or 2 to quit, and indeed when you enter 2 the loop will end, but also when the user enters 3 or any other number. Even worse, if a user will enter a character that is not a number or nothing at all, they will get an exception. For this we use try-except clauses. another way to do this kind of loop is using "while "True" and then using "break" to exit.

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")

Upvotes: 1

Roim
Roim

Reputation: 3066

If you don't care about execution time but only about code length:


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")

Did not really make it smarter, just shorter by creating a list and take option as index in the list

Upvotes: 2

Related Questions