2
\$\begingroup\$

My program generates URLs. It takes in 3 variables and substitutes them in a URL to create different working URLs. It also has 3 options for language selection, which change the language of the page to which the URL points.

However, the more options I add the more copy - pasted code there is. I know there is a way to simplify the code and only add the changes, however I'm still new to Python and I'm not too sure how.

#created by Kaloian Kozlev on 10.08.2018

import replit
import time


def welcome():
    choice = 0
    while choice <= 5:
        print(
            "\nWelcome to URL Generator v3.0                         VERSION UPDATE: 31/08/2018       \n--------------------------------                      --------------------------          \n\n1.Direct/Combined     2.Indirect                      1. Multiple offer link added \n3.Console             4.Facebook                      2. Facebook link added \n                                                      3. Multipe language support  \n5.Exit \n--------------------------------                      -------------------------- "
        )

        try:
            print("\n1. English \n2. German\n3. French ")
            lang = int(input("\nSelect Language:  "))

            #English
            if lang == 1:

              choice = int(input("\nSelect URL generator: "))

              if choice == 1:
                  multiple = str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                        str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

                  if multiple =="n":
                    print("\n\nSingle offer link\n-----------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                          str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

              elif choice == 2:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

              elif choice == 3:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=en-GB")

              elif choice == 4:
                  rid = int(input("\nPlease enter the RID: "))
                  pid = int(input("Please enter the PID: "))
                  print(
                    "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&pid=" + str(pid) + "&lang=en-GB")

            #German
            if lang == 2:

              choice = int(input("\nSelect URL generator: "))

              if choice == 1:
                  multiple = str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                        str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

                  if multiple =="n":
                    print("\n\nSingle offer link\n-----------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                          str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

              elif choice == 2:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

              elif choice == 3:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=de-DE")

              elif choice == 4:
                  rid = int(input("\nPlease enter the RID: "))
                  pid = int(input("Please enter the PID: "))
                  print(
                    "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&pid=" + str(pid) + "&lang=de-DE")

            #French
            if lang == 3:
              choice = int(input("\nSelect URL generator: "))

              if choice == 1:
                  multiple = str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                        str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

                  if multiple =="n":
                    print("\n\nSingle offer link\n-----------------")
                    cid = str(input("\nPlease enter the CID: "))
                    rid = int(input("Please enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print("\nhttps://bda.bookatable.com/?cid=" + cid + "&rid=" +
                          str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

              elif choice == 2:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=INTL-LBDIRECTORY_INDIRECT:10508&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

              elif choice == 3:
                  multiple =str(input("\nWould you like to create a multiple offer link? Y/N "))

                  if multiple == "y":
                    print("\n\nMultiple offer link\n-------------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = str(input("Please enter multiple PIDs using ****,****: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

                  if multiple == "n":
                    print("\n\nSingle offer link\n-----------------")
                    rid = int(input("\nPlease enter the RID: "))
                    pid = int(input("Please enter the PID: "))
                    print(
                        "\nhttps://bda.bookatable.com/?cid=CONSOLEEMAILCAMPAIGNS:18663&rid="
                        + str(rid) + "&pid=" + str(pid) + "&lang=fr-FR")

              elif choice == 4:
                  rid = int(input("\nPlease enter the RID: "))
                  pid = int(input("Please enter the PID: "))
                  print(
                    "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&pid=" + str(pid) + "&lang=fr-FR")

              elif choice == 5:
                  replit.clear()
                  print("\n\nProgram terminated")
                  return
              else:
                  print("\nWrong selection, please choose options 1-5")
                  time.sleep(2)
                  replit.clear()
                  welcome()

            end = input("\nWould you like to continue Y/N ")
            if end == "y":
                replit.clear()
            if end == "n":
                replit.clear()
                print("\n\nProgram terminated")
                return
            elif end == "":
                replit.clear()
                print("\n\nProgram terminated")
                return

        except ValueError:
            print("\nPlease enter a number!")
            time.sleep(1)
            replit.clear()


welcome()

(This code is also in repl.it.)

\$\endgroup\$
1
\$\begingroup\$

More functions. If you are copy and pasting code it should be in a fucntion call, not another repeat.

Your call to welcome() should be underneath an if __name__ == '__main__': block as it is pythonic.

You have three different languages but the execution is all the same so you only need a third of the code that you have.

You make another call to welcome inside of it, but without any utility gained, you just plop another namespace on top which is not an effective solutoin. I would recommend doing something like running this in a loop until you get your desired input.

If you want to be generating URLs then typically they can be chuncked and treated as concatenations of the different parts of URLs, rather than hard coding lots of pieces over and over.

You have a block if multiple = y or = n, but then inside you have almost identical code, why is the multiple not a conditional later -- these lines also do not have to be repeated.

You also don't have to create a query for multiple vs singular, a user can just keep entering PIDs until they say that they have finished. This logic will be a lot kinder.

Your very long print string would be much more readable as a multi-line string, or even you could use the textwrap library.

'This is one'
'singular'
'long string in python. It will be parsed as if there are'
'no breaks in it\n except for where I put\n newlines!!'
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.