3
\$\begingroup\$
def alphabetlist():
 print("alphabet1 = [a,b,c,d,e,f]")
 print("alphabet2 = [g,h,i,j,k,l]") 
 print("alphabet3 = [m,n,o,p,q,r]") 
 print("alphabet4 = [s,t,u,v,w,x]")
 print("alphabet5 = [y,z]")
print("Guess a word.")
print("Now i will try to figure out the word you guessed.")
totalletters=int(input("total number of letters in your guessed words are: "))
def chooselist(x):
 if x == 1:
 print(alphabet1)
 elif x == 2:
 print(alphabet2)
 elif x == 3:
 print(alphabet3)
 elif x == 4:
 print(alphabet4)
 elif x == 5:
 print(alphabet5)
list=[ ]
def listtranspose(w):
 if w == 1:
 i=int(input());list.append(alphabet1[i-1])
 elif w == 2:
 i=int(input());list.append(alphabet2[i-1])
 elif w == 3:
 i=int(input());list.append(alphabet3[i-1])
 elif w == 4:
 i=int(input());list.append(alphabet4[i-1])
 elif w == 5:
 i=int(input());list.append(alphabet5[i-1]) 
alphabetlist()
alphabet1 = ["a","b","c","d","e","f"]
alphabet2 = ["g","h","i","j","k","l"] 
alphabet3 = ["m","n","o","p","q","r"] 
alphabet4 = ["s","t","u","v","w","x"]
alphabet5 = ["y","z"]
step_1=[ ];h=0
while totalletters>h:
 print("your letter is from which list 1,2,3,4,5")
 x = int(input())
 step_1.append(x);h+=1
print("***\n 1 2 3 4 5 6 ")
g=0
while totalletters>g:
 chooselist(step_1[g]);g+=1
print("*******");e=0
while totalletters>e:
 print("choose from these lists again now 1,2,3,4,5,6")
 listtranspose(step_1[e]);e+=1
print("the word you guessed is: ",end=" ")
for i in range(0,len(list),1):
print(list[i],end="")

This program is a word guessing game. The user thinks of a word and this program guesses it. through a series of list I want to know if there are other ways to write this, more easily actually I get all the functions used but not the next part

Chocolate
1,0145 silver badges21 bronze badges
asked Jan 2, 2021 at 9:33
\$\endgroup\$
1
  • 2
    \$\begingroup\$ The current question title, which states your concerns about the code, applies to too many questions on this site to be useful. The site standard is for the title to simply state the task accomplished by the code. Please see How do I ask a good question?. \$\endgroup\$ Commented Jan 2, 2021 at 11:37

2 Answers 2

2
\$\begingroup\$

This looks like a nice game. Let's see how we can improve your code.

  • we can STOP using ;. This is Python, not C / C++ / Java etc
  • reorder your code so that it reads easier.
  • rename your functions / variables so that the words are delimited by _.
  • we can use the string stdlib library to generate your alphabets without repeating ourselves so much:
import string
LETTERS = string.ascii_lowercase
LETTERS = [
 list(LETTERS[letter:letter + 6])
 for letter in range(0, len(LETTERS), 6)
]

The above will generate:

[['a', 'b', 'c', 'd', 'e', 'f'], ['g', 'h', 'i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p', 'q', 'r'], ['s', 't', 'u', 'v', 'w', 'x'], ['y', 'z']]

Just by following the above and adapting some of the functions we'd get:

def alphabet_list():
 for index, letters_block in enumerate(LETTERS, start=1):
 print("alphabet{} = {}".format(index, letters_block))
def choose_list(choice):
 # since you wanted to count from 1, we have to do this
 # e.g. if user wants the 2nd list, we'll look at index 1
 choice = choice - 1
 try:
 return LETTERS[choice]
 except IndexError:
 print("Value has to be between 1 and {}".format(len(LETTERS)))
 sys.exit()

Your while loops can also be written as for loops which are more idiomatic for what you're trying to achieve. For example, the step_1 while can be written like (not taking into account the proper exception handling):

step_1 = [
 int(input('What list does your letter belong to: '))
 for _ in range(user_word_length)
]

Something that I've seen pretty often is that some are confused about the order of the execution. In Python, execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom. Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called.

That said, if you have something like this:

def function_1():
 print("function_1")
def function_2():
 print("function_2")

Nothing is going to be printed unless you call those functions. Now depending on your workflow, you can call which ever you want first.

I wanted to point this because you can define all your functions at the top of your file, and then write the rest of the logic. Having statements between your functions makes your code so hard to understand.

answered Jan 2, 2021 at 14:50
\$\endgroup\$
1
  • \$\begingroup\$ can you please help me a bit more I'm new to programming \$\endgroup\$ Commented Jan 2, 2021 at 18:14
3
\$\begingroup\$

I don't know what you mean by "more easily", but here are some comments on the code:

No error handling

In many statements like i=int(input());list.append(alphabet1[i-1]) you use user input directly to index a list, without checking if it is in range, or even if it is a number.
One wrong keystroke from the user and the whole program crashes.

Mixed code

You mix main script code with function definitions, making it harder to follow your program flow by hand, thus making your program harder to understand.

You should have your main program code follow all the functions in one coherent block, and it is even better to wrap it in its own main function, then use a construct like this:

if __name__ == '__main__':
 main()

Repetitive code

In both listtranspose and chooselist you write the same line 5 times over, just because you used separate variables for 5 (almost) identical lists instead of a list of lists.

Naming

Do not use list as a variable name! It is a Python builtin for creating new lists, and while Python is nice enough to allow you to overwrite it, this can both break future code and make your code harder to understand by others.

There are seemingly random one letter variables in your code h, g, e, it is hard to understand what they do.
Please give them more meaningful names.

Function alphabetlist actually prints the lists, so it would be better to name it print_lists or similar. Also, there is no reason to hardcode the lists you print, since they already exists as list type variables.

answered Jan 2, 2021 at 12:33
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Overall good points. For repetitive code and error handling, you should offer alternative strategies to fix the issues you've identified. \$\endgroup\$ Commented Jan 2, 2021 at 14:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.