How to add a value to each variable in a list

Question:

I want to create a program, where you have join words. Example:

  1. Computer asks: Write this word in French: (english word, generated randomly)

  2. You answer: (the word in French)

    • IF IT IS CORRECT:

      1. Computer: correct
    • IF IT IS INCORRECT:

      1. Computer: incorrect

I want to have setted multiple words (like 50 words) and I want computer to pick randomly and i wanna have for every world to have this word in French. (I wanna set the words manually)

I hope that this question is not that messy as I thing. I didnt knowed how to ask it, so hope thats good.

Thanks for all the answers. Filip Dvorak

Asked By: Filip Dvorak

||

Answers:

this should do the trick:

import random

words = {"yes": "wi","no": "no"} #you need to manually enter 50 words hre
shuffeled_keys = list(words.keys())
random.shuffle(shuffeled_keys)
for eng_word in shuffeled_keys:
    fr = input("how do you say %s in France"%eng_word)
    if fr == words[eng_word]:
        print("correct")
    else:
        print("incorrect")

if you have any questions feel free to ask me in the comments, and if my code helped you, please consider marking it as the answer 🙂

Answered By: Ohad Sharet

you should write dictionary of words, i will write a script for you to get the idea clear:

import random

dict = {'apple' : 'pomme', 'banana':'banane','car':'auto'}
english_words = list(dict)
print(english_words)

max = len(dict) - 1

while(1):
    en = english_words[random.randint(0,max)]
    fr = input('Write '+en+' in French: ')
    if(dict[en] == fr):
        print('correct')
    else:
        print('incorrect')
    
Answered By: Abd Albary Taraqji

Well, you could first have a dict with the english word as key and their translation in french as the values. It can look like this

words = {
    "word": "mot",
    "hello": "bonjour"
}

Then you could take input and just run an if statement. And for the english words you can get easily get all of the keys of the dict using the keys method.

import random
engWords = list(words.keys)
randomWord = random.choice(engWords)

inp = input(f"Enter the french translation of this word - {randomWord} - ")
if inp == words[inp]: print("Your answer is correct")
else: print("You answer is incorrect")


Answered By: Debarka Naskar

So, assuming that you want to make an app that asks the user to write an English word in French, lets do this…

Action plan:

i) store all the English words in a list
ii) store all the answers for the words in a list respective to the English words…
iii) show the English word to user and ask him the French equivalent
iv) alert the user about his/her result

Just remember I’m using simple python without complex terms…

I’ve provided comments for step by step explanation:

# import the random module because we want to ask random words
import random

# creating the lists
all_english_words = []
answers = []

# we want the program to run continuously, so use a while loop
# set it to true as we want to run infinitely
while True:
    # generating a random number between 0 and the number of words in list
    question = random.randint(0, len(all_english_words) - 1)
    # getting the question and answer according to the randomly generated number
    current_question = all_english_words[question]
    current_answer = answers[question]
    
    # asking the user to guess
    guess = input("What's the French word for " + current_question + "?")
    # checking if the guess is correct, first convert everythig to lowercase
    if guess.lower() == current_answer.lower():
        print("Hooray! You were right! The word was " + current_answer)
    else:
        print("Aww no, you were wrong... :(")
        # if you want to tell the user correct answer, comment out the next line
        # print("The French word for " + current_question + " is " + current_answer)

Example questions:

all_english_words = ["hello", "hi", "sun", "moon", "children"]
answers = ["Bonjour", "Salut", "soleil", "lune", "enfants"]

I’m not good in French but yes, its a great idea for your first project!
Hope this helps my lad! Keep it up!

Answered By: Vishu
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.