Untitled
unknown
plain_text
3 years ago
9.1 kB
4
Indexable
# import module random
import random
# create a variable 'points' to count user points and create lists for different types of messages depending on user results
points = 0
correct_input_messages = ['Awesome!', 'Great!','Fantastic!','You got it!']
incorrect_input_messages = ['Sorry!', 'That was wrong!']
score_mesaages = ['Score: ', 'Score is: ', 'Your score is: ']
# create DNA strand list of 4 letters
strands_list = ['C' , 'G' , 'A' , 'T']
# print welcome message and ask user for name input
print("Welcome to the DNA quiz game!")
name_input = input("Enter your username >> ")
def integer_input(prompt):
""" input: int
return: int
verifies that the input by the user is an int
"""
# create a user_input variable assigned for the variable witht he function perimeter (prompt)
user_input = input(prompt)
# while True, if the input is an int, return the value
while True:
if user_input.isnumeric():
return int(user_input)
# print error message
user_input = input("Invalid input, please try again >>> ")
def generate_sequence(length):
""" input: str
return: str
generates random length of DNA strands from strands_list based on user input
"""
sequence = random.choices(strands_list, k = length)
# use ''.join to join each element in the list
return ''.join(sequence)
def compute_complement(sequence):
""" input: str
return: str
function for complement that examines a given sequence and computes the correct results
"""
# create a dictionary for the DNA strands
complement_dict = {
'A': 'T',
'T': 'A',
'C': 'G',
'G': 'C'
}
# create an empty string to compute the correct complement for a given sequence
complement = ""
for base in sequence:
complement += complement_dict[base]
return complement
def compute_reverse(sequence_1):
""" input: str
return: str
function for reverse that reverses a given sequence
"""
# create an empty string to compute the correct reverse for a given sequence
result = ""
for base in sequence_1:
result = base + result
return result
def compute_compress(sequence_2):
""" input: str
return: str and int
fucntion for compress that compresses for a given sequence
"""
# create a count variable assigned as 0 and an empty string to store sequences
count = 0
compressed_string = ""
# for each element in sequence_2, if characters repeat, then increment a counter, if you don't see a repeat then write the counter and the character to the result string
for i in range(len(sequence_2)):
current_base = sequence_2[i]
if i < (len(sequence_2) - 1):
next_base = sequence_2[i + 1]
if next_base == current_base:
count += 1
else:
if count > 0:
compressed_string += str(count + 1)
count = 0
compressed_string += current_base
else:
if count > 0:
compressed_string += str(count + 1)
count = 0
compressed_string += current_base
return compressed_string
def compute_expand(sequence_3):
""" input: str and int
return: str
fucntion for expand that expands the strands for a given sequence
"""
# create an empty string and create a for loop that checks if a symbol is numeric and if it is, it multiplies it by the next character, if not then the elements in i get added to the sequence
expanded_str = ""
for i in range(len(sequence_3)):
if sequence_3[i].isnumeric():
expanded_str += ((int(sequence_3[i]) - 1) * sequence_3[i + 1])
else:
expanded_str += sequence_3[i]
return expanded_str
# ask user for DNA length input using the integer_input
DNA_length_input = integer_input(f"Hi {name_input}, please enter a positive integer for the DNA length >> ")
# while above is True, if the DNA input is less then or euqual to 0, print error input message; else, continue with the option message
while True:
if DNA_length_input <= 0:
DNA_length_input = integer_input("Invalid input, please try again >>> ")
else:
print("\nSelect an option [1-4] to answer a question or 5 to quit the game.\nWin the game by scoring at least 10 points! \n ")
break
# create a variable and st it to -1
option_input = -1
# while -1 does not equal 5, run the loop
while option_input != 5:
# ask user for te options
option_input = int(input("\n1. Complement [2 points] 2. Reverse [2 points] 3. Compress [3 points] 4. Expand [3 points] 5. Quit\n> "))
# if the input is between 1 and 5, execute the applicable statemnts below
if option_input >= 1 and option_input <= 5:
# if option_input is 1, generate the random DNA sequence given by the user DNA length and ask for the complement question
# store the answer by user inside the function to verify if the answer if correct or not
if option_input == 1:
sequence = generate_sequence(DNA_length_input)
complement_input = input(f"What is the complement of {sequence}? ").upper()
complement = compute_complement(sequence)
# if the user complement answer is correct, award them 2 points and display score and message
if complement_input == complement:
points += 2
print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" )
# otherwise, subtract points by 1 and display the score and message
else:
points -= 1
print (f"{random.choice(incorrect_input_messages)} The correct answer is {complement} You now have {points} points. \nPlay again?")
# if option_input is 2, generate the random DNA sequence given by the user DNA length and ask for the reverse question
# store the answer by user inside the function to verify if the answer if correct or not
if option_input == 2:
sequence_1 = generate_sequence(DNA_length_input)
reverse_input = input(f"What is the reverse of {sequence_1}? ").upper()
reversed_sequence = compute_reverse(sequence_1)
# if the user reverse answer is correct, award them 2 points and display score and message
if reverse_input == reversed_sequence:
points += 2
print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" )
# otherwise, subtract points by 1 and display the score and message
else:
points -= 1
print (f"{random.choice(incorrect_input_messages)} The correct answer is {reversed_sequence} You now have {points} points. \nPlay again?")
# if option_input is 3, generate the random DNA sequence given by the user DNA length and ask for the compress question
# store the answer by user inside the function to verify if the answer if correct or not
if option_input == 3:
sequence_2 = generate_sequence(DNA_length_input)
compression_input = input(f"What is the compression of {sequence_2}? ").upper()
compressed = compute_compress(sequence_2)
# if the user reverse answer is correct, award them 3 points and display score and message
if compression_input == compressed:
points += 3
print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" )
# otherwise, subtract points by 1 and display the score and message
else:
points -= 1
print (f"{random.choice(incorrect_input_messages)} The correct answer is {compressed} You now have {points} points. \nPlay again?")
# if option_input is 4, generate the random DNA sequence given by the user DNA length and ask for the expand question
# store the answer by user inside the function to verify if the answer if correct or not
if option_input == 4:
sequence_3 = compute_compress(generate_sequence(DNA_length_input))
expanded_input = input(f"What is the expanded form of {sequence_3}? ").upper()
expanded = compute_expand(sequence_3)
# if the user expand answer is correct, award them 3 points and display score and message
if expanded_input == expanded:
points += 3
print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" )
# otherwise, subtract points by 1 and display the score and message
else:
points -= 1
print (f"{random.choice(incorrect_input_messages)} The correct answer is {expanded} You now have {points} points. \nPlay again?")
# if the user does NOT enter within the range of 1-5, display error message and run the loop again until the user inputs the correct range
else:
print("Please enter a digit between [1-5]")
# if score is ewual to or above 10, display congratulations message and show the updated points to user; otherwise, show the sorry message and the updated points.
if points >= 10:
print(f"\nGreat job {name_input}, you won the game with {points} points! \nGoodbye!")
else:
print(f"\nSorry {name_input}, you didn’t win this time with the total score of {points}. \nGoodbye")Editor is loading...