Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Clean the code - Language: Python Your friend, Bob, has written a random program, playing around with the basics of Python. But for such simple

Clean the code - Language: Python

image text in transcribed

image text in transcribed

image text in transcribed

Your friend, Bob, has written a random program, playing around with the basics of Python. But for such simple operations, he feels as though his file is way too long. He remembers hearing from someone that, when you're trying to program something really simple, you'll often find that "long code is wrong code". Even if the code is working properly, making it shorter would make it much better and cleaner to handle. Bob's file is named al_warmup.py. All Bob wants is to make the file shorter. He doesn't want the way the code works to change. Could you clean up this code for him, using functions and getting rid of other useless things? (For example, by putting all the mathematical calculations into one equation instead of splitting each step up, or, when checking if a boolean value is True, instead of writing the redundant code 'if (bool == True)', simply writing 'if (bool)', etc.). Try to get the file down to less than 40 lines. import random fave_number = 1 # doing some calculations on this number new_fave = fave_number + fave_number new_fave = new_fave * 2 new_fave = new_fave ** 5 new_fave = new_fave + 123456 print(new_fave) # check if new_fave is divisible by 5 div_by_5 = False if (new_fave % 5 == 0): div_by_5 = True else: div_by_5 = False if (div_by_5 == True): print("Is new fave number divisible by 5? True") elif (div_by_5 == False): print("Is new fave number divisible by 5? False") fave_number_2 = 42 # doing the same calculations as above on this number new_fave_2 = fave_number_2 + fave_number_2 new_fave_2 = new_fave_2 * 2 new_fave_2 = new_fave_2 ** 5 new_fave_2 = new_fave_2 + 123456 print(new_fave_2) # check if new_fave_2 is divisible by 5 div_by_5 = False if (new_fave_2 % 5 == 0): div_by_5 = True else: div_by_5 = False if (div_by_5 == True): print("Is new fave number #2 divisible by 5? True") elif (div_by_5 == False): print("Is new fave number #2 divisible by 5? False") fave_word = 'hello' # add 'world' and a random number (between 0-3) of 'Bob's to this word fave_word += "world" rand_num = random.randint(0,3) if rand_num == 0: fave_word += " elif rand_num == 1: fave_word += 'Bob' elif rand_num == 2: fave_word += 'BobBob' elif rand_num == 3: fave_word += 'BobBob Bob' else: fave_word += " print(fave_word) fave_word_2 = 'csc108' # add 'world' and a random number (between 0-3) of 'Bob's to this word fave_word_2 += "world" rand_num = random.randint(0,3) if rand_num == 0: fave_word_2 += " elif rand_num == 1: fave_word_2 += 'Bob' elif rand_num == 2: fave_word_2 += 'BobBob' elif rand_num == 3: fave_word_2 += 'BobBobBob' else: fave_word_2 += " print(fave_word_2) fave_word_3 = 'catz' # add 'world' and a random number (between 0-3) of 'Bob's to this word fave_word_3 += "world" rand_num = random.randint(0,3) if rand_num == 0: fave_word_3 += " elif rand_num == 1: fave_word_3 += 'Bob elif rand_num == 2: fave_word_3 += 'BobBob' elif rand_num == 3: fave_word_3 += 'BobBobBob' else: fave_word_3 += " print(fave_word_3)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions