Try to forget everything that you know about the code, and assume that you aren't the author. Imagine yourself to be a third person reading the code, he/she knows nothing about it and just reading it for the first time.
ui = userinput()
choosef(ui[0],ui[1])
Alright, userinput()
must take some input and put it into ui
. Makes sense, but what is choosef()
?.
Naming convention
Use meaningful names for functions and variables. take this example
x = 3.14
y = 24
what is x
and y
to the reader? Is that the price of something?
pi_value = 3.14
number_of_hours = 24
Ahhh okay, now I know exactly what 3.14
means. So If I use it somewhere the reader will also know what I exactly mean. This is an important part of writing clean code.
Another aspect is the style. There are a few ways I can write user input*
user_input()
USERINPUT()
UserInput()
userinput()
Which one should I follow?
To have a consistent naming convention, python code follows the PEP-8 naming convention. When I say follows, I mean that it is recommended that you should follow it too as other python libraries also use this. It makes the code look cleaner.
Things like functions follow : lower_snake_case
Classes follow: CamelCase
You can read the link for more information.
Taking input in Python
print('Enter your name:')
n = input()
Clearly, you want to display a message to the user before he enters something. This is why the input()
function has something called input prompt.
name = input("Enter your name: ")
You can display the message between the ()
. This removes the additional line. Also note that I changed n
to name
for the reasons I mentioned above.
Formatting strings
From your code, I can see that you concatenated strings with +
to form meaningful sentences. That works but there is a huge problem when you want to use variables of different types.
name = "Eric"
age = 14
job = "Comedian"
print("Hello " + name + "You are " + age + " years old and you are a " + comedian)
TypeError: can only concatenate str (not "int") to str
Simply use Python 3's f-strings. Place the letter 'f' before "
print(f"Hello {name}, you are {age} years old ")
Cleaner.
return value from userinput()
What you are currently doing is returning a tuple of choice
and name
. It's better that you keep them as they are because later on, you slice from it just to get them again.
This means you directly do
return name, choice
# Calling the function
name, choice = userinput()
Code structure
Couple of points
Do not ask "Do you want to play again? "
in the play_again()
function. The reason you called that function should be because the user wants to play again. Move that to a playgame()
function which will call user_input()
every time the user wants to play the game, and break from the loop only if the user enters 'n'
or "no"
spaces = spaces + 1
can be simplified into spaces += 1
Move the part where you count the number of characters into a separate function which will return an integer. So your play_game()
function doesn't do anything other than play the game. When you need the characters , number_of_char = character_len( name )
.
Use enum for clarity.
An improved version of the code
from enum import Enum
class PrintChoices(Enum):
number_of_char = 'c'
reversed_name = 'r'
exit_choice = 'e'
def find_num_of_char(name):
return len(name) - name.count(' ')
def reverse_name(name):
return name[::-1]
def user_input():
name = input("Enter your name: " )
choice = input("Would you like to (r)everse your name\n or would you like to print the number of (c)haracters or (e)xit?: ")
return name, choice
def clear_screen():
print(chr(27) + "[2J")
def play_game():
while True:
clear_screen()
name, choice = user_input()
if choice == PrintChoices.reversed_name.value:
print(reverse_name(name))
input("Press any key to continue...")
elif choice == PrintChoices.number_of_char.value:
print(find_num_of_char(name))
input("Press any key to continue...")
elif choice == PrintChoices.exit_choice.value:
break
else:
input("Invalid input, Press any key to continue...")
play_game()
Note: I have also added print(chr(27) + "[2J")
to clear the screen.