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

Time:11-04

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")

CodePudding user response:

  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") 

CodePudding user response:

my Answer:

import random

list = ["a1", "a2"]
op1 = random.choice(list)
print(op1)
if op1 == "a1":
   op2 = "a2"
else:
   op2 = "a1"

 
print("Is the Sky Blue ? If Yes type")
if op1 == "a1":
   print("a1")
else:
   print("a2")
print("If NO type")
if op1 == "a2":
   print("a1")
else:
   print("a2")


test = input("your guess :")

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