Question

Python 3 Can anyone make my Python code more readable, by breaking up the code into...

Python 3

Can anyone make my Python code more readable, by breaking up the code into smaller chunks using functions. If a piece of code tries to do several things, it should be broken up into several different functions.

print("chatbot: Hello, I am chatbot. What is your name?")

name = input("")

print("chatbot: Nice to meet you "+name+". Tell me now, what is your age?")

age = int(input(""))

if age >= 16 and age <= 25:
print("We are the same age! What a coincidence!")

else:
if age < 16:
print("chatbot: You're younger than me! Try to guess my age?")

else:
print("chatbot: You are older than me! Try to guess my age?")

bot_age = int(input(""))

if bot_age > 16 or bot_age < 25:
print("chatbot: Nope! My age is between 16 and 25. Try again.")
bot_age =int(input(""))

while bot_age!= 22:
print("chatbot: Nope! My age is between 16 and 25. Try again.")
bot_age = int(input(""))

print("chatbot: That's right! Hooray! Let's play a game now, tell me a year and I will tell you if it's a leap year or not");

year = int(input())

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

print("chatbot: It's a leap year!")

else:

print("chatbot: It's not a leap year")

print("chatbot: Give me two numbers x and y! First give x press enter, and then y!")

x = int(input(""))

y = int(input(""))

print("chatbot: If we do x/y then quotient will be "+str(int(x/y))+" and remainder will be "+str(x % y))

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Python code:

# define function for self introduction like name
def personName(name):
print("chatbot: Nice to meet you "+name+". Tell me now, what is your age?")
  
# define function for self introduction like age
def personAge(age):
if age >= 16 and age <= 25:
print("We are the same age! What a coincidence!")
else:
if age < 16:
print("chatbot: You're younger than me! Try to guess my age?")

else:
print("chatbot: You are older than me! Try to guess my age?")

# define function for bot introduction like age
def botAge(bot_age):
if bot_age > 16 or bot_age < 25:
print("chatbot: Nope! My age is between 16 and 25. Try again.")
bot_age =int(input(""))

while bot_age!= 22:
print("chatbot: Nope! My age is between 16 and 25. Try again.")
bot_age = int(input(""))
  
# define function for check year is leap year or not
def checkYear(year):
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
print("chatbot: It's a leap year!")
else:
print("chatbot: It's not a leap year")
  
# define function for find the quotient and remainder
def divideNumber(x,y):
print("chatbot: If we do x/y then quotient will be "+str(int(x/y))+" and remainder will be "+str(x % y))
  
# main function
if __name__=="__main__":
  
print("chatbot: Hello, I am chatbot. What is your name?")
name = input("")
# call the function personName
personName(name)
  
age = int(input(""))
# call the function personAge
personAge(age)
  
bot_age = int(input(""))
# call the function botAge
botAge(bot_age)
  
print("chatbot: That's right! Hooray! Let's play a game now, tell me a year and I will tell you if it's a leap year or not");
year = int(input())
# call the function checkYear
checkYear(year)

print("chatbot: Give me two numbers x and y! First give x press enter, and then y!")
x = int(input(""))
y = int(input(""))
# call the function divideNumber
divideNumber(x,y)

Python code screenshot:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Python 3 Can anyone make my Python code more readable, by breaking up the code into...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • on python i need to code a guessing game. After the player has guessed the random...

    on python i need to code a guessing game. After the player has guessed the random number correctly, prompt the user to enter their name. Record the names in a list along with how many tries it took them to reach the unknown number. Record the score for each name in another list. When the game is quit, show who won with the least amount of tries. this is what i have so far: #Guess The Number HW assignment import...

  • how would i write my code in Pseudocode? I am pretty much a beginner, therefore this...

    how would i write my code in Pseudocode? I am pretty much a beginner, therefore this is my first major computer science assignment and im not really understanding the written way of code.. please help and explain. Thank you! this is my working code: # Guess The Number HW assignment import random guessesTaken = 0 names=[] tries=[] print("Welcome! ") question = input("Would you like to play the guessing game? [Y/N]") if question == "N": print("Sorry to see you go so...

  • In python how could I update my code to tell me the number of guesses it...

    In python how could I update my code to tell me the number of guesses it took my to get the correct number here is my working code so far import random number = random.randint(1,100) total_guess = 0 while total_guess<=100: guess = int(input("what is your guess?")) print (guess) total_guess = total_guess + 1 if guess<number: print("Too low") if guess>number: print("Too high") if guess==number: print("Correct!") break

  • [PYTHON] Create a chatbot program in Python: a program that appears to talk intelligently to a...

    [PYTHON] Create a chatbot program in Python: a program that appears to talk intelligently to a human using text. Your program should involve asking the user questions and having the computer respond in a reasonably intelligent fashion based on those answers. For example, here is a sample chat: ChatBot: Welcome, I am Chatbot . What is your name? User: My name is Abby . ChatBot: Hello Abby. How old are you? User: 22. <---- Input ChatBot: That is older than...

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • What is wrong with my python code. I am receiving this error: Traceback (most recent call...

    What is wrong with my python code. I am receiving this error: Traceback (most recent call last): File "C:/Users/owner/Documents/numberfive.py", line 7, in if age > 18: TypeError: unorderable types: str() > int() My code is age= input("Enter your age:") if age > 18: print("You can vote.") else: print("You can't vote.") if age > 64: print ("You're a Senior Citizen...you deserve two votes") endofprogram = input("Please hit enter to exit from this program:")

  • Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot...

    Hello, I am trying to solve the following problem using python: Assignment 3: Chatbot A chatbot is a computer program designed to emulate human conversation. For this program you will use if statements, user input, and random numbers to create a basic chatbot. Here is the scenario: You have decided to start an online website. You are creating a prototype to show investors so you can raise money and launch your website. You should ask the user at least 5...

  • for this code fix the smtax error this is a python code for some reason my...

    for this code fix the smtax error this is a python code for some reason my code isnt liking my returns and it bring up other symtax error so fix the following code answer should pop out the following when runned 1/ 2 + 1/ 3 + 1/ 12 = 11/12 z=' ' def math(x,y):     global z     if y==0 or x==0:         return     if y%x ==0:         z=("1/"+str(int(y/x))         return      if x%y ==0         z+=(int(x/y))    ...

  • FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called...

    FIX CODE-- import random number=random.randint ('1', 'another number') print("Hello, CIS 101") print("Let's play a guessing Game!called guess my number.") print("The rules are: ") print("I think of a number and you'll have to guess it.") guess = random.randint(1, 5) print("You will have " + str(guess) + " guesses.") YourNumber = ??? unusedguesses=int(guess) while unusedguesses < guess:     if int(YourNumber) == number:         print("YAY, You have guessed my number")     else:         unusedguesses=int(guess)-1         if unusedguesses == 0:              break         else:             print("Try again!") print("The number was ", str(number))

  • Part 1: Python code; rewrite this code in C#, run the program and submit - include...

    Part 1: Python code; rewrite this code in C#, run the program and submit - include comments number= 4 guesscount=0 guess=int(input("Guess a number between 1 and 10: ")) while guess!=number: guesscount=guesscount+1 if guess<number: print("Your guess is too low") elif guess>number: print("Your guess is too high") else: print("You got it!!") guess=int(input("Guess again: ")) print("You figured it out in ",guesscount," guesses") Part 2: C++ code; rewrite the following code in C#. Run the program and submit. - include comments #include <iostream> using...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT