4
\$\begingroup\$

I created a code that will create, or rather build a URL based on the configuration. Everything works, however with the expansion of my project I added multiple languages etc. every section only changes very small details in the output however I copy and pasted the majority of the code. (I'm still learning and this was the only way I knew how to do it). I don't know how to get rid of the nested if statements and reuse the majority of the code. I know that I can use functions, however I can't imagine where I can begin and how they will exchange the information between themselves.

#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("\nLanguage:\n---------\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:
              offer = str(input("\nWould you like to create an Offer or Restaurant link? O/R"))

              if offer == "o":
                print("\n\nFacebook offer link\n-------------------")
                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")

              if offer == "r":
                print("\n\nFacebook restaurant link\n------------------------")
                rid = int(input("\nPlease enter the RID: "))
                print(
                  "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&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:
              offer = str(input("\nWould you like to create an Offer or Restaurant link? O/R"))

              if offer == "o":
                print("\n\nFacebook offer link\n----be created with min - ---------------")
                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")

              if offer == "r":
                print("\n\nFacebook restaurant link\n------------------------")
                rid = int(input("\nPlease enter the RID: "))
                print(
                  "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&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:
              offer = str(input("\nWould you like to create an Offer or Restaurant link? O/R"))

              if offer == "o":
                print("\n\nFacebook offer link\n-------------------")
                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")

              if offer == "r":
                print("\n\nFacebook restaurant link\n------------------------")
                rid = int(input("\nPlease enter the RID: "))
                print(
                  "\nhttps://bda.bookatable.com/?cid=UK-RES-FACEBOOK:24747&rid="+ str(rid)+ "&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()
            welcome()

        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()
\$\endgroup\$
1
\$\begingroup\$

Getting it running

Currently the code that's posted does not run - it fails with a SyntaxError. This part can be easily overcome by changing the single quotes here

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

into triple quotes like this for a multiline string:

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

Triple quotes is one of the ways python allows you to enter multiline string literals in your code.

Fixing the infinite loop

This code

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

is an infinite loop.

When run, the string literal is printed over and over again forever since the boolean expression governing the while loop always evaluates to True. It seems to me that you want the part of retreiving a user's input to be in a while loop. Perhaps something like this would work:

lang = 0
print("\nLanguage:\n---------\n1. English \n2. German\n3. French ")
while lang == 0:
    try:
        lang = int(input("\nSelect Language:  "))
        print("\nThanks..\n")
    except:
        print("\nPlease enter a number!")

choice = 0 
while choice == 0:
    try:
        choice = int(input("\nSelect URL generator: "))
        print("\nThanks..\n")
    except:
        print("\nPlease enter a number!")

This code will continue asking for the language selection from the user until he or she enters input that can be successfully cast to an integer. It does the same for the choice input. You might want to also implement logic to only allow the user to continue (break out of the while loop) if he or she enters not just any input that can be cast to an integer, but one of the few integers that your program is expecting (ie. language should be 1, 2 or 3.).

Notice the keep point that each while loop can be exited since the boolean expression governing it can be satisfied if the user enters the input you're expecting and the loop terminates.

General Pointers

  • Be consistent in your indentation - choose either two or four spaces and be consistent in using one or the other throughout the program. This will make your code more readable and more easy to work with.
  • Just as you implemented a try-except pattern for checking that the user provides an integer for the language selection, you will want to do the same throughout your program - whenever you're using this pattern: int(input("Please enter ..")). As written, your program will error out if a user enters in any input that not an integer in these scenarios. Since this is a commonly needed operation in your program, you should implement this try-except pattern to retrieve user input in a general-use function to be used over and over.
  • You might want to allow your user-entered strings to be any case. This would allow your tool to be more user friendly. Something like this multiple =str(input("\nWould you like to create a multiple offer link? Y/N ")).lower() would do the trick.
\$\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.