![]() |
code problems - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: code problems (/thread-11381.html) |
code problems - lokchi2017 - Jul-05-2018 is there any problems in this code, because it says invalid syntax question1 = input("Question!") answer1 = input("Answer of question1") question2 = input("Question!") answer1 = input("Answer of question2") import random random = random.randint(1,2) if random = 1: playeranswer1 = input(question1) if playeranswer1 = answer1: print("you are right") else: print("you are wrong") else: playeranswer2 = input(question2) if playeranswer2 = answer2: print("you are right") else: print("you are wrong")also i want to make a question generator that generates random questions from google. is there any way to connect python to google? and is there any way for me to search google through python code? also i have another problem , and it is i want to run the code again after i answer the questions. but if the random.randint generates the same number as the one that is already asked, it will generate another one. how to achieve this? thanks! RE: code problems - Zombie_Programming - Jul-06-2018 You get invalid syntax because of your if statements. When comparing two values you should use == so your code should look like this:import random question1 = input("Question!") answer1 = input("Answer of question1") question2 = input("Question!") answer2 = input("Answer of question2") random = random.randint(1,2) if random == 1: playeranswer1 = input(question1) if playeranswer1 == answer1: print("you are right") else: print("you are wrong") else: playeranswer2 = input(question2) if playeranswer2 == answer2: print("you are right") else: print("you are wrong")When it comes to your problem with the random number generation, here's an example that is general. Essentially it will generate a random number 10 times (you can change this obviously) and if the number hasn't already been generated, it will add the number to the accepted numbers list(You can use this instead to generate a question) and also add the number to number_list. The number_list keeps track of the numbers already generated. If the randomly generated number is in number_list, then it needs to generate another number. I append it to not_accepted_numbers for showing it in this example, but you can just keep it to generating another number. import random number_list = [] # Used to store already generated numbers accepted_numbers = [] # Used to store numbers not in the number list not_accepted_numbers = [] # Used to store numbers already in the number list # Generate a random number between 1 - 50 random_number = random.randint(1, 50) # Generate 10 accepted numbers for i in range(10): # Loop till an accepted number is generated while True: # If the number isnt in number list, accept the number # Generate a new random number if random_number not in number_list: accepted_numbers.append(random_number) number_list.append(random_number) random_number = random.randint(1, 50) break # If the number is in number list, do not accept the number else: not_accepted_numbers.append(random_number) random_number = random.randint(1, 50) # Print out the list of accepted numbers and not accepted numbers print(accepted_numbers) print(not_accepted_numbers)Here's an example run of the program: Also, PLEASE use the python tags when posting your code it makes it easier to copy the code and allow people to help you. It also makes the post much easier to read and distinguish what is what on your post.
RE: code problems - lokchi2017 - Jul-06-2018 print("okay")like python /python(with []) is this a python tag RE: code problems - Zombie_Programming - Jul-06-2018 (Jul-06-2018, 01:39 AM)lokchi2017 Wrote:Yes, when you put code into your post, wrap it in python tags like you did there.print("okay")like python /python(with []) is this a python tag |