Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code problems
#1
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!
Reply
#2
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:
Output:
[9, 39, 30, 42, 35, 21, 10, 44, 15, 7] [39]
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.
Reply
#3
print("okay")
like python /python(with [])
is this a python tag
Reply
#4
(Jul-06-2018, 01:39 AM)lokchi2017 Wrote:
print("okay")
like python /python(with []) is this a python tag
Yes, when you put code into your post, wrap it in python tags like you did there.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code problems bbm007 5 1,154 Apr-20-2023, 08:36 PM
Last Post: deanhystad
  multiple problems with code SrijaRamarthy 2 1,859 Nov-06-2019, 06:24 AM
Last Post: SrijaRamarthy
  code editor/ IDE problems nameno1had 2 3,314 Mar-12-2018, 10:09 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020