import random
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 += 'BobBobBob'
else:
fave_word += ''
print(fave_word)
Looking for a way to turn this into a function and cut down on redundant code. (python)
I have written the code using PYTHON PROGRAMMING LANGUAGE.
OUTPUT:


CODE:
import random #imported random module
#Function definition for favword
def favword():
fave_word = 'hello'#defined fave_word
fave_word += "world"#adding world to fave_word
#To generate random number in between 0 to 3 inclusive
rand_num = random.randint(0,3)
#To print message on console
print("Random number : "+str(rand_num) )
fave_word += "Bob"*rand_num#to add Bob
#To print message on console
print("Favourite word : "+fave_word)
if(__name__ == "__main__"):
favword()#Calling favword
Thanks..
import random fave_word = 'hello' # add 'world' and a random number (between 0-3) of 'Bob's...
Try to get the code down to less than 40 lines. (PYTHON) 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...
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))
Can you please enter this python program code into an IPO Chart? import random def menu(): 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 non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...
python: how would I format the grid below so that it is aligned correctly? as you see, I tried to center it but it is still not correct. import random row=random.randint(1,10) col=random.randint(1,10) for r in range(row): for c in range(col): if r %2==0 and c %2==0: print(format('a','^3'),end=' ') elif r %2!=0 and c %2!=0: print(format("/x\\",'^3'),end=' ') elif c%2!=0: print(format('-','^3'),end='') else: print(format('s','^3'),end=' ') print() #go to next line current Output is: (example if random function choses col=4 and row=3)...
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...
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...
import random
def doTest(operation):
## complete your work here ##
# return True for now
return True
responsesCorrect = 0
print("The software will process a test with 10 questions ……
")
for compteur in range (10):
operation = random.randint(0,1)
if doTest(operation) == True:
responsesCorrect += 1
print(responsesCorrect, "Correct responses")
if responsesCorrect <= 6 :
print("Ask some help from your instructor.")
else:
print("Congratulations!")
Requirement: You must use the format provided below and
then complete the fill! Python! Thanks!...
Python Language: Please see program listed below of a game called: Rock, Paper, Scissors. Please add an exception, for example if the user inputs something else other than rock, paper or scissors, the exception will print message: "Enter only rock, paper, or scissors". Also add option to keep playing the game, for example: print: "Would you like to play again? Enter Y or N". If the user says Y, the game will repeat, if N, the game will end and...
PYTHON import random # VARIABLES first_num = random.randint(1, 100) sec_num = random.randint(1, 100) totalsum = first_num + sec_num def additon(first_num, sec_num): return first_num + sec_num; # INTRO print("Hello, \n" "this is an app that helps you practice your math skills while generating 6 sets of lucky numbers") question = input('Are you up for a challenge? [Y/N]') if question == 'n': print('okay, see you later') if question == 'y': print("Let's start!!") # USER NAME VALUE uname = input('what is your user...
Hello! I'm looking for help with this assignment. Complete a program in pseudocode and Python that performs the following tasks. Open the file called M4Lab1ii.py linked below these instructions in your M4 Content module in IDLE. Save as M4Lab1ii.py. Replace ii with your initials. [example for someone with the initials cc: M4Lab1cc.py] Complete Steps 1-7 as requested within the code’s comments. Run your program and test all four calculations, then exit the program. Save your program as M4Lab1ii.py using your...