I'm now currently learning Python because of school projects and I'm trying to make an element guessing game based on the info given. Sorry if my English is bad cause I'm not native English speaker. Also first time using this platform lol.
This is the code
import random
#Element and description
astatine = "Rarest naturally occurring element in the Earth crust"
bromine = "Naturally occurring element that is a liquid at room temperature besides Mercury"
bismuth = "Most naturally diamagnetic element"
radon = "Densest noble gases"
tungsten = "Has the highest melting point of all metals"
radium = "Heaviest alkaline earth metal"
caesium = "Most reactive metal"
neodymium = "Strongest permanent magnet"
fermium = "Heaviest element that can be formed by neutron bombardment of lighter elements"
oganesson = "Highest atomic number of all known elements"
#Listing out the variables
list = [astatine, bromine, bismuth, radon, tungsten, radium, caesium, neodymium, fermium, oganesson]
#Opening
input('Welcome to the element guessing game. Guess the element based on the info given. Press enter to continue.')
#Randomise the list
while True:
randomdesc = random.choice(list)
#Print the question
print('↓↓↓\n' , '>>> ' , randomdesc , ' <<<')
#Answer
ans = str(input('Type your answer here → ')).lower
#check if the answer is same as the element
if ans == list:
selection = input('Correct. Press 1 to play again. Press Any key to exit program')
else:
selection = input('Incorrect, Press 1 to play again. Press Any key to exit program.')
if selection == "1":
continue
print('thanks')
break
The problem I currenly facing is I have no idea how to check whether the answer typed is correct or not. Whatever i type, it always show "Incorrect, Press 1 to play again. Press Any key to exit program."
My ideal result is:
Welcome to the element guessing game. Guess the element based on the info given. Press enter to continue.
↓↓↓
>>> Most reactive metal <<<
Type your answer here → caesium
Correct. Press 1 to play again. Press Any key to exit program
thanks
But instead the result is:
Welcome to the element guessing game. Guess the element based on the info given. Press enter to continue.
↓↓↓
>>> Most reactive metal <<<
Type your answer here → caesium
Incorrect, Press 1 to play again. Press Any key to exit program.
thanks
What I want is when lets say the question is "Most reactive metal", and i should type caesium. But the problem is when it comes to check the answer, the code should be
if ans == the element chosen:
But i couldn't find a way to fit that inside. If i put "if ans == list:" it will always be incorrect no matter what i type. Or there's anyway to make the variable able to check the equality (not only strings) such as "if ans == caesium:" but the result is also same as previous one.
Or is there a way to reverse the variables and strings such as
caesium = "Most reactive metal"
into
"Most reactive metal" = caesium
Conclusion, I stucked at the "if ans == something else:" part and i need to find a way to make it work so if the answer is correct, it will show "Correct. Press 1 to play again. Press Any key to exit program" and if the answer is wrong then it's the opposite.
Thank you if you are willing to help <3
CodePudding user response:
You have several misunderstandings here. You can't use the name of a variable as a string. What you need is a dictionary, where both key and value are strings. Next, you were using ...).lower
, but without the parentheses, so the result was the lower
function object, not the result of calling the function.
This does what you want:
import random
elements = {
"astatine": "Rarest naturally occurring element in the Earth crust",
"bromine": "Naturally occurring element that is a liquid at room temperature besides Mercury",
"bismuth": "Most naturally diamagnetic element",
"radon": "Densest noble gases",
"tungsten": "Has the highest melting point of all metals",
"radium": "Heaviest alkaline earth metal",
"caesium": "Most reactive metal",
"neodymium": "Strongest permanent magnet",
"fermium": "Heaviest element that can be formed by neutron bombardment of lighter elements",
"oganesson": "Highest atomic number of all known elements"
}
input('Welcome to the element guessing game. Guess the element based on the info given. Press enter to continue.')
#Randomise the list
while True:
elem = random.choice(list(elements))
print(elem)
print('↓↓↓\n' , '>>> ' , elements[elem] , ' <<<')
ans = input('Type your answer here → ').lower()
#check if the answer is same as the element
if ans == elem:
selection = input('Correct. Press 1 to play again. Press Any key to exit program')
else:
selection = input('Incorrect, Press 1 to play again. Press Any key to exit program.')
if selection == "1":
continue
print('thanks')
break