Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a python program that displays a welcome message and general instructions

ID: 3863093 • Letter: W

Question

Write a python program that displays a welcome message and general instructions to the user for the bulls and cows game. (Hint: call a function that outputs the welcome and instructions.) Ask the player whether he/she wished to play or exit. If exit is entered, end the game. If not, start a new game. Randomly generate an integer with 4 unique digits. Each digit must be in the range of 1 to 9. Ask the player to enter his/her guess. Verify that the player entered 4 digits, if not 4 digits request the player reenter a valid number. (use a while loop to continue to ask the user for a valid input). Calculate and display the results of the number of bulls and cows. Continue the game until the correct guess is made or the player wishes to quit (do this in a loop). If the guess is correct congratulate the player, if the player "quits" the game prematurely then simply display the number and end the game. Ask the player whether they want to continue or exit. Start a new game or exit (must be done in a loop).

This is for python and I have not learned lists yet. Thank you.

Explanation / Answer

#main Controller
def main():
print "Welcome to Bulls and Cows game! ";
choice = raw_input('Do you wish to start a new game. y/n?');
  
if choice=='y' or choice=='Y':
startGame();
  
  
print " Thank you for Playing Bulls and Cows ";
  
  
def startGame():
print " -------------Starting game------------- ";
displayRules();
# The value that needs to be guessed
masterValue = generateRandomNumber();
  
while(True):
choice = raw_input(' Enter your guess or type "bye" to exit game');
# when exit word is entered it exits python terminal hence, i choose the 'bye' word.
if choice=='bye':
print " Thank you for playing with us.The number was "+masterValue;
break;
else:
#loops until a valid guess
while(isValidGuess(choice)):
choice = raw_input(' Enter a valid guess according to rule');
  
#checks masterValue and valid guess
if masterValue == choice:
print "You Win! Your Guess was right";
break;
else:
print "Invalid Guess! Try Again";
  

#function check valid guess
def isValidGuess(inp):
# number should be 4 digit number
if(len(inp)!=4):
return True;
# number should not be alphabets
if(not inp.isdigit()):
return True;
#matches first digit with rest 3
matchesFirst = inp[0]==inp[1] or inp[0]==inp[2] or inp[0]==inp[3];
#matches second digit with rest 2
matchesSecond = inp[1]==inp[2] or inp[1]==inp[3];
#matches third digit with fourth digit
matchesThird = inp[2]==inp[3];
  
if matchesFirst or matchesSecond or matchesThird:
#return true so that it asks for another choice
return True;
  
#return false so that it exists the loop
return False;

def generateRandomNumber():
import random;
num = "";
#loops until a four digit number with unique digit is genrated
while(len(num)!=4):
nextDigit = str(random.randrange(0,10));
# check if nextDigit is already choose.If yes then it re-generates a new digit
num += "" if nextDigit in num else nextDigit;
return num;


def displayRules():
# diplay rules and instruction. As you wish
print " To win you will have guess an integer with 4 unique digits. Each digit must be in the range of 1 to 9.";

#begins the execution of the program
main();

# Thanks Happing Coding :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote