Untitled
unknown
plain_text
2 years ago
4.1 kB
2
Indexable
Never
import random points = 0 positive_messages = ['Awesome!', 'Great!','Fantastic!','You got it!'] negative_messages = ['Sorry!', 'That was wrong!'] score_mesaages = ['Score: ', 'Score is: ', 'Your score is: '] word_bank = ['C' , 'G' , 'A' , 'T'] print("Welcome to the DNA quiz game!") name_input = input("Enter your username >> ") DNA_length_input = int(input(f"Hi {name_input}, please enter a positive integer for the DNA length >> ")) def generate_sequence(length): sequence = random.choices(word_bank, k = length) return ''.join(sequence) def compute_complement(sequence): complement_dict = { 'A': 'T', 'T': 'A', 'C': 'G', 'G': 'C' } complement = "" for base in sequence: complement += complement_dict[base] return complement def compute_reverse(sequence_1): result = "" for base in sequence_1: result = base + result return result def compute_compress(sequence_2): count = 0 compressed_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 while True: if DNA_length_input <= 0: error_input = int(input("Invalid input, please try again >>> ")) DNA_length_input = error_input else: break option_input = -1 while option_input != 5: option_input = int(input("\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 \n1. Complement [2 points] 2. Reverse [2 points] 3. Compress [3 points] 4. Expand [3 points] 5. Quit\n> ")) if option_input >= 1 and option_input <= 5: 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 complement_input == complement: points += 2 print(random.choice(positive_messages), random.choice(score_mesaages), points) else: points -= 1 print (f"{random.choice(negative_messages)} The correct answer is {complement} You now have {points} points. \nPlay again?") 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 reverse_input == reversed_sequence: points += 2 print(random.choice(positive_messages), random.choice(score_mesaages), points) else: points -= 1 print (f"{random.choice(negative_messages)} The correct answer is {reversed_sequence} You now have {points} points. \nPlay again?") if option_input == 3: sequence_2 = generate_sequence(DNA_length_input) compression_input = input(f"What is the compression of {sequence_2}? ").upper() # s = sequence_2 compressed = compute_compress(sequence_2) if compression_input == compressed: points += 3 print(random.choice(positive_messages), random.choice(score_mesaages), points) else: points -= 1 print (f"{random.choice(negative_messages)} The correct answer is {compressed} You now have {points} points. \nPlay again?") if option_input == 4: pass else: pass else: print("Invalid input, please try again!") 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")