Untitled
unknown
plain_text
2 years ago
5.0 kB
3
Indexable
import random 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: '] strands_list = ['C' , 'G' , 'A' , 'T'] print("Welcome to the DNA quiz game!") name_input = input("Enter your username >> ") def integer_input(prompt): while True: user_input = input("Invalid input, please try again >>> ") if user_input.isnumeric(): return int(user_input) def generate_sequence(length): sequence = random.choices(strands_list, 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 def compute_expand(sequence_3): 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 DNA_length_input = integer_input(f"Hi {name_input}, please enter a positive integer for the DNA length >> ") 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 option_input = -1 while option_input != 5: option_input = int(input("\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 (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" ) 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 == 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 (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" ) 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 == 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 compression_input == compressed: points += 3 print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" ) 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 == 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 expanded_input == expanded: points += 3 print (f"{random.choice(correct_input_messages)} {random.choice(score_mesaages)} {points} \nPlay again?" ) else: points -= 1 print (f"{random.choice(incorrect_input_messages)} The correct answer is {expanded} You now have {points} points. \nPlay again?") else: print("Please enter a digit between [1-5]") 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...