1
\$\begingroup\$

I'm new to python and PEP 8, so I want to see how to improve my work. This is my first big project without a guide. This is 184 lines. I think I murdered PEP 8 and need help fixing that.

import random
name = "null"
jokes_list = ["Why did the chicken cross the road? To get to the other side.", "The only time incorrectly isn’t spelled incorrectly is when it’s spelled incorrectly.",
"I intend to live forever... or die trying.", "A blind man walks into a bar....And a table, and a chair.",
"A clear conscience is usually the sign of a bad memory", "Your life.", "Why don’t you ever see hippopotamus hiding in trees? Because they’re really good at it.",
"What kind of shoes do ninjas wear? Sneakers.", "Why did the lifeguard kick the elephants out of the pool? They kept dropping their trunks.",
"When is a door not a door? When it's ajar.", "Why do golfers wear two pairs of pants? In case they get a hole in one."]
functions = "I have many functions including help, math, settings, jokes, games, and quit."
def start_up():
 print("powering on...")
 print("Hi I'm ChatterBot.")
 name = input("What is your name: ")
 print(f"Hi {name}.")
 return name
def help(name):
 print(f"What do you need help with {name}.")
 sub_function = input("functions or : ").lower()
 if sub_function == 'functions':
 print(functions)
 else:
 print(f"I can't help you with that {name}.")
def settings(name):
 print ("What setting")
 sub_sub_function = input("name or restart: ").lower()
 if sub_sub_function == 'name':
 name = input("What should I call you then: ")
 if sub_sub_function == 'restart':
 print("Powering down...")
 name = "Null"
 current_function = "Null"
 sub_function = "Null"
 sub_sub_function = "Null"
 num1 = "0"
 num2 = "0"
 start_up()
 else:
 print(f"That is not on of my settings {name}.")
 return name
def math(name):
 op = input("What operation|+ - / *|: ")
 if op == '+':
 num1 = input("What is the first number: ")
 num2 = input("What is the second number: ")
 answer = int(num1) + int(num2)
 print(f"Your answer is: {answer}")
 if op == '-':
 num1 = input("What is the first number: ")
 num2 = input("What is the second number: ")
 answer = int(num1) - int(num2)
 print(f"Your answer is: {answer}")
 if op == '*':
 num1 = input("What is the first number: ")
 num2 = input("What is the second number: ")
 answer = int(num1) * int(num2)
 print(f"Your answer is: {answer}")
 if op == '/':
 num1 = input("What is the first number: ")
 num2 = input("What is the second number: ")
 answer = int(num1) + int(num2)
 print(f"Your answer is: {answer}")
def jokes(name = name):
 print(jokes_list[random.randint(0,10)])
def games(names = name):
 print('What game do you want to play')
 game = input('High Or Low or Rock Paper scissors: ').lower()
 if game == 'highorlow' or game == 'high or low':
 high_or_low()
 if game == 'rockpaperscissors' or game == 'rock paper scissors':
 rock_paper_scissors(name)
def high_or_low():
 print('Loading...')
 print('High Or Low')
 yn = 'Null'
 yn = input('Do you need me to explain the rules. Y/N: ').lower()
 if yn == 'y' or yn == 'yes':
 print('I will think of a number between 1 - 100.')
 print('You will guess a number and I will tell you if it is higher or lower than my number.')
 print('This repeats until you guess my number.')
 play_hol = True
 num = random.randint(0,101)
 move_count = 0
 while play_hol:
 try:
 move_count += 1
 guess = input("What number do you guess: ")
 if int(guess) == num:
 print(f'You won in {move_count} moves.')
 play_hol = False
 if int(guess) > num:
 print("Lower")
 if int(guess) < num:
 print("Greater")
 except Exception as e:
 print(f"Pick a number ", e)
def rock_paper_scissors(name):
 play_rps = True
 while play_rps:
 rps = ["rock", 'paper', 'scissors']
 player_rps = input("Rock, Paper, or Scissors: ").lower()
 com_rps = rps[random.randint(0,3)]
 print(com_rps)
 if com_rps == player_rps:
 print('Tie')
 if com_rps == 'rock' and player_rps == "scissors":
 print("Chatter Bot Wins!")
 if com_rps == 'scissors' and player_rps == "paper":
 print("Chatter Bot Wins!")
 if com_rps == 'paper' and player_rps == "rock":
 print("Chatter Bot Wins!")
 if player_rps == 'rock' and com_rps == "scissors":
 print(f"{name} Wins!")
 if player_rps == 'sicssors' and com_rps == "paper":
 print(f"{name} Wins!")
 if player_rps == 'paper' and com_rps == "rock":
 print(f"{name} Wins!")
 yn = input("Do you want to play again. Y/N: ").lower()
 if yn == 'n' or yn == 'no':
 play_rps = False
name = start_up()
while True:
 print(f"What do you want to do {name}.")
 print(functions)
 current_function = input("Functions: ").lower()
 if current_function == 'help':
 help(name)
 if current_function == 'settings':
 name = settings(name)
 if current_function == 'quit':
 print("powering down...")
 quit()
 if current_function == 'math':
 math(name)
 if current_function == 'jokes':
 jokes(name)
 if current_function == 'games':
 games(name)
 else:
 print(f"That is not one of my functions {name}.")
asked Feb 18, 2019 at 17:56
\$\endgroup\$
6
  • \$\begingroup\$ Someone already pointed out how to improve the rock paper scissors.codereview.stackexchange.com/questions/213738/… \$\endgroup\$ Commented Feb 18, 2019 at 17:58
  • \$\begingroup\$ I'd suggest replacing the RPS code here with your improved code. \$\endgroup\$ Commented Feb 18, 2019 at 18:00
  • \$\begingroup\$ I'm having trouble adding the rock, paper scissors fix to my code. \$\endgroup\$ Commented Feb 18, 2019 at 18:21
  • \$\begingroup\$ @Peilonrayz I'm not the one who voted to close as "not working", but I see that asking for suggestions about what to add next is asking for advice about code that has not yet been implemented, and that would be problematic for Code Review. \$\endgroup\$ Commented Feb 18, 2019 at 18:24
  • \$\begingroup\$ I fixed that and see your point @200_success. \$\endgroup\$ Commented Feb 18, 2019 at 18:35

1 Answer 1

2
\$\begingroup\$

Some suggestions:

  1. When I copy/paste the code into a file locally, I have problems with Python reading the source encoding. You didn't specify an encoding, and your joke text is full of non-ascii characters.

    I'd suggest that you store your jokes in a separate file, and read in that file either at startup or each time the joke function is called.

  2. You have to ask the user's name in two places: during startup, and when changing the name in the settings. Therefore, you need a get_name() function to be called both places.

  3. I'm not sure what the restart operation is about. But Python uses None to indicate no value, so that might be better than settings things to "Null".

  4. Your math function is bad. Write an expression parser instead. ;-)

  5. Don't catch Exception. It's a bad practice. Catch the specific error you intend to handle.

  6. You have a bunch of bare code at the bottom of the file. Organize that into a main routine, and then do:

    if __name__ == '__main__':
     main()
    

    Doing this will allow you to import the module without going into the chatterbot directly, which means you can write unit tests for the various parts of the system.

answered Feb 18, 2019 at 19:12
\$\endgroup\$
2

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.