Jul-06-2020, 04:02 AM
Here is a part of my code for one of my assignment in programming.
When I try the program I run into a certain problem, when the answer contains (for example) black, and the user enters black twice in the input, the program sees it as 2 correct colour.
How do I improve my program to prevent this from happening!
colour =['red','blue','green','white','orange','grey','yellow','black','pink']
#selecting colour from the pre set list
import random
c1=random.choice(colour)
c2=random.choice(colour)
c3=random.choice(colour)
c4=random.choice(colour)
answer = [c1,c2,c3,c4]
print(answer)
#explaining the game to the user
print('Welcome to the game of Mastermind, in this game 4 colours are choosen randomily, but the sequence of the colours matter.')
print('The goal of the game is to guess those colours along with their respective positions')
print('The list of colurs are', colour, 'please write in lower case ONLY')
print('After every try, you will be prompted about the number of correct colours and the number of colours in the right position')
print('Your code is ready now try your best to figure it out with at little attempts as you can!')
attempt=1
#asking user to make a guess
g1=input('please guess the first colour: ')
while g1 not in colour:
print ('please input colour from the list of given colours!')
g1=input('please guess the first colour: ')
g2=input('please guess the second colour: ')
while g2 not in colour:
print ('please input colour from the list of given colours!')
g2=input('please guess the second colour: ')
g3=input('please guess the third colour: ')
while g3 not in colour:
print ('please input colour from the list of given colours!')
g3=input('please guess the third colour: ')
g4=input('please guess the fourth colour: ')
while g4 not in colour:
print ('please input colour from the list of given colours!')
g4=input('please guess the fourth colour: ')
#comparing the users input to the pre determined answer
col=0
if g1 in answer:
col=col+1
if g2 in answer:
col=col+1
if g3 in answer:
col=col+1
if g4 in answer:
col=col+1
When I try the program I run into a certain problem, when the answer contains (for example) black, and the user enters black twice in the input, the program sees it as 2 correct colour.
How do I improve my program to prevent this from happening!
colour =['red','blue','green','white','orange','grey','yellow','black','pink']
#selecting colour from the pre set list
import random
c1=random.choice(colour)
c2=random.choice(colour)
c3=random.choice(colour)
c4=random.choice(colour)
answer = [c1,c2,c3,c4]
print(answer)
#explaining the game to the user
print('Welcome to the game of Mastermind, in this game 4 colours are choosen randomily, but the sequence of the colours matter.')
print('The goal of the game is to guess those colours along with their respective positions')
print('The list of colurs are', colour, 'please write in lower case ONLY')
print('After every try, you will be prompted about the number of correct colours and the number of colours in the right position')
print('Your code is ready now try your best to figure it out with at little attempts as you can!')
attempt=1
#asking user to make a guess
g1=input('please guess the first colour: ')
while g1 not in colour:
print ('please input colour from the list of given colours!')
g1=input('please guess the first colour: ')
g2=input('please guess the second colour: ')
while g2 not in colour:
print ('please input colour from the list of given colours!')
g2=input('please guess the second colour: ')
g3=input('please guess the third colour: ')
while g3 not in colour:
print ('please input colour from the list of given colours!')
g3=input('please guess the third colour: ')
g4=input('please guess the fourth colour: ')
while g4 not in colour:
print ('please input colour from the list of given colours!')
g4=input('please guess the fourth colour: ')
#comparing the users input to the pre determined answer
col=0
if g1 in answer:
col=col+1
if g2 in answer:
col=col+1
if g3 in answer:
col=col+1
if g4 in answer:
col=col+1