Y
Y
Yanlax2021-10-23 20:08:47
Python
Yanlax, 2021-10-23 20:08:47

How can I make it ask a question first and then call a specific function?

import pyshorteners
import os
import platform
import qrcode


class URL():
    
    action = int(input(" What to do? (1.QrCode 2.ShortUrl) "))

    def short_url():

        url = input(" URL: ")
        n = input(" Choice service ( 1:Os.db, 2:Chilp.it, 3:Da.gd, 4:Is.gd, 5:Qps.ru, 6:TinyURL.com):  ")

        if n == str(1):
            s = pyshorteners.Shortener()
            f = s.osdb.short(url)
            print(" Short URL: ",f)
        elif n == str(2):
            s = pyshorteners.Shortener()
            short_url = s.chilpit.short(url)
            print(" Short URL: ",short_url)
        elif n == str(3):
            s = pyshorteners.Shortener()
            abz = s.dagd.short(url)
            print(" Short URL: ",abz)
        elif n == str(4):
            s = pyshorteners.Shortener()
            abv = s.isgd.short(url)
            print(" Short URL: ",abv)
        elif n == str(5):
            s = pyshorteners.Shortener()
            abh = s.qpsru.short(url)
            print(" Short URL: ",abh)
        elif n == str(6):
            s = pyshorteners.Shortener()
            abk = s.tinyurl.short(url)
            print(" Short URL: ",abk)
        else:
            print(' Error ')

    short_url()


    def qr_code():

        URL = input(" URL: ")
        file = input(" Enter file name:")

        filename = file +".png"

        img = qrcode.make(URL)
        img.save(filename)

    qr_code()

    if action == 1:
        qr_code()
    elif action == 2:
        short_url()
    else:
        print(" Error ")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
InternetMaster, 2021-10-23
@InternetMaster

So stick functions out of plain code and only use them at the end of the "if".

import pyshorteners
import os
import platform
import qrcode


class URL():
    
    action = int(input(" What to do? (1.QrCode 2.ShortUrl) "))

    def short_url():

        url = input(" URL: ")
        n = input(" Choice service ( 1:Os.db, 2:Chilp.it, 3:Da.gd, 4:Is.gd, 5:Qps.ru, 6:TinyURL.com):  ")

        if n == str(1):
            s = pyshorteners.Shortener()
            f = s.osdb.short(url)
            print(" Short URL: ",f)
        elif n == str(2):
            s = pyshorteners.Shortener()
            short_url = s.chilpit.short(url)
            print(" Short URL: ",short_url)
        elif n == str(3):
            s = pyshorteners.Shortener()
            abz = s.dagd.short(url)
            print(" Short URL: ",abz)
        elif n == str(4):
            s = pyshorteners.Shortener()
            abv = s.isgd.short(url)
            print(" Short URL: ",abv)
        elif n == str(5):
            s = pyshorteners.Shortener()
            abh = s.qpsru.short(url)
            print(" Short URL: ",abh)
        elif n == str(6):
            s = pyshorteners.Shortener()
            abk = s.tinyurl.short(url)
            print(" Short URL: ",abk)
        else:
            print(' Error ')

    def qr_code():

        URL = input(" URL: ")
        file = input(" Enter file name:")

        filename = file +".png"

        img = qrcode.make(URL)
        img.save(filename)

    if action == 1:
        qr_code()
    elif action == 2:
        short_url()
    else:
        print(" Error ")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question