dimanche 9 septembre 2018

Problem with creating a loop with undefined varibales checking a defined list of parameters, using split, if, list and hopefully loop

"Bond, James Bond" - name-returning program The idea behind this program is that you write your name, and the program returns your name in similar fashion to have James Bond represents himself So if you write your name it is supposed to be returned as:

"The name is Bond, James Bond"

If you write "James Bond" as your name. However, if your name is, f.ex. "Grace Murray Hopper", then it returns:

"The name is Hopper, Grace Murray Hopper"

If your name ends on "jr, sr or roman numbers (I only added up to III)," like Henry Huxleberry III, it returns:

"The name is Huxleberry, Henry Huxleberry III"

And if the name includes "Von, Van, De or Di (I havent added any more)", like Jan Von Neumann, it returns:

"The name is Von Neumann, Jan Von Neumann"

If it includes both of the past mentioned parts, like "Jan Von Neumann II" it returns:

"The name is Von Neumann, Jan Von Neumann II

All of this is working, but the code is long, and if I want to make it functioning for longer names I need to add it manually.

So I believe that there is a way I can write the code shorter, where I am not depending on defined variables for each component in the name, only the list including "Jr, Sr and Roman Numbers", and "Van, Von, De and Di ++", and i hope for the program to work for "infinitly" long names But I dont understand the loop-function, nor the "if any(c in list for c in name)" function Id be gratefull for any and all help! Thank you.

TL;DR I want help with understanding how I make a program that does as the following program does, but is not dependant on fixed variables a, b, c, d ++, Any help would be appreciated! I am new to programming, Ive only been doing this for two days

name = str(input("Write your name: "))
lst = name.split(" ")
stringcount = len(lst)
check_a = list("jr" or "sr" or "Jr" or "Sr" or "I" or "II" or "III")
check_b = list("Van" or "Von" or "van" or "von" or "De" or "Di" or "de" or "di")

def namecheck(name):
    if int(stringcount) == 0:
        print("Im going to need your name, Sir.")
    elif int(stringcount) == 1:
        print("The name is " + str(name) + ", " + str(name) + ".")
    elif int(stringcount) == 2:
        a, b = name.split()
        if b == "jr" or b == "sr" or b == "Jr" or b == "Sr" or b == "I" or b == "II" or b == "III":
            print("The name is " + str(a) + ", " + str(name) + ".")
        elif a == "Van" or a == "Von" or a == "van" or a == "von" or a == "De" or a == "Di" or a == "de" or a == "di":
            print("The name is " + str(b) + ", " + str(name) + ".")
        else:
            print("The name is " + str(b) + ", " + str(name) + ".")
    elif int(stringcount) == 3:
        a, b, c = name.split()
        if c == "jr" or c == "sr" or c == "Jr" or c == "Sr" or c == "I" or c == "II" or c == "III":
            print("The name is " + str(b) + ", " + str(name) + ".")
        elif b == "Van" or b == "Von" or b == "van" or b == "von" or b == "De" or b == "Di" or b == "de" or b == "di":
            print("The name is " + str(b) + " " + str(c) + ", " + str(name) + ".")
        else:
            print("The name is " + str(c) + ", " + str(name) + ".")
    elif int(stringcount) == 4:
        a, b, c, d = name.split()
        if d == "jr" or d == "sr" or d == "Jr" or d == "Sr" or d == "I" or d == "II" or d == "III":
            if b == "Van" or b == "Von" or b == "van" or b == "von" or b == "De" or b == "Di" or b == "de" or b == "di":
                print("The name is " + str(b) + " " + str(c) + ", " + str(name) + ".")
            else:
                print("The name is " + str(c) + ", " + str(name) + ".")
        elif b == "Van" or b == "Von" or b == "van" or b == "von" or b == "De" or b == "Di" or b == "de" or b == "di":
            print("The name is " + str(b) + " " + str(c) + " " + str(d) + ", " + str(name) + ".")
        elif c == "Van" or c == "Von" or c == "van" or c == "von" or c == "De" or c == "Di" or c == "de" or c == "di":
            print("The name is " + str(c) + " " + str(d) + ", " + str(name) + ".")
        else:
            print("The name is " + str(d) + ", " + str(name) + ".")
    else:
        g_name = name.split(" ", stringcount)[stringcount-1]
        print("The name is " + str(g_name) + ", " + str(name) + ".")

namecheck(name)

Im using "Python IDLE 3.7"

Aucun commentaire:

Enregistrer un commentaire