Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i randomize my answers, so that it syncs with the if else

i am trying to built a little game with random. In this game you have the choice to answer a question with "a1" and "a2" for "Answer 1" and "Answer 2". At the End it should print, if you chose the right Answer. To make it a little more dynamic i want that the Option for "a1" and "a2" is not always the same. Like the Answer, if the sky ist Blue shouldn’t be always "a1". If would be nice if somebody helps me with my code.
Thanks

import random
from random import randint

list = ["a1", "a2"]
op1 = random.choice(list)
op2 = ""

if op1 == "a1":
    op2 = "a2"
else:
    op1 = "a2"


print("Is the Sky Blue ? If Yes type")
print("a1")
print("If NO type")
print("a2")

test = "a1" # user input 

if test == op1:
    print("Yeah thats right")
elif test == op2:
    print("thats the wrong answer")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. You don’t don’t need to from random import randint because already you import random library.

  2. if op1 == "a1", op2 == "a2", else: op1 will be "a2" and you need to add op2 = "a1".Because of you have 2 options in list.

And your code will be like this :

import random

list = ["a1", "a2"]
op1 = random.choice(list)

if op1 == "a1":
    op2 = "a2"
else:
    op2 = "a1"
     

test = input("your guess :")

if test == op1:
    print("Yeah thats right")
elif test == op2:
    print("thats the wrong answer") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading