Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP ME!!
#1
I got some problems with creating the following python programme. I want to let python to calculate the sum by itself while the student fills in the answer. Help me please!! (the programme is in dutch, so if u don't understand something because of that. Just ask!)


def Toets ():

    class Vragen:
        def __init__(self, vraag, antwoord, antwoord2):
            self.vraag = vraag
            self.antwoord = antwoord
            self.antwoord2 = antwoord2

    b = 1
    c = 5
    a**2 = c-b
    a**2 = a*a


   vragen_list = [
   ["Los de vergelijking x**2 + 1 = 5 op. \n \nRestrictie: U mag geen spaties in uw antwoord gebruiken. \nNoteer uw antwoord zo nodig in de volgende vorm: x=.. v vx v teller/noemer v x**2.",
    "x=2vx=-2", "x=-2vx=2"],
    ]
   vragen = []

   for item in vragen_list:
        vragen.append(Vragen(item[0],item[1],item[2]))
    
   def run_test(vragen):
        for vragen in vragen :
            print(vragen.vraag)
            antwoord = input("\nAntwoord: ")
            if antwoord == "a":
                print("\nCorrect!\n")
                break
            if antwoord == "x=-2vx=2":
                print("\nCorrect!\n")
                break
            if antwoord == "x=2":
                print ("\nFout, niet vergeten dat er ook nog een negatieve x-waarde uit kan komen.\n\n")
                Toets ()
                break
            if antwoord == "x=-2":
                print ("\nFout, niet vergeten dat er ook nog een positieve x-waarde uit kan komen.\n\n")
                Toets ()
                break
            if antwoord == ("x=v2") :
                print ("\nFout, je hebt twee keer de wortel genomen van 5 - 1 = 4.\n\n")
                Toets ()
                break
            if antwoord == ("x=v2vx=-v2") :
                print ("\nFout, je hebt twee keer de wortel genomen van 5 - 1 = 4.\n\n")
                Toets ()
                break
            if antwoord == ("x=-v2vx=v2") :
                print ("\nFout, je hebt twee keer de wortel genomen van 5 - 1 = 4.\n\n")
                Toets ()
                break
            if antwoord == ("x=v4vx=-v4") :
                print ("\nFout, je kan je antwoord namelijk nog verder uitwerken.\n\n")
                Toets ()
                break
            if antwoord == ("x=-v4vx=v4") :
                print ("\nFout, je kan je antwoord namelijk nog verder uitwerken.\n\n")
                Toets ()
                break
            if antwoord == ("x=v4") :
                print ("\nFout, je kan je antwoord namelijk nog verder uitwerken.\n\n")
                Toets ()
                break
            if antwoord == ("x=-v4") :
                print ("\nFout, je kan je antwoord namelijk nog verder uitwerken.\n\n")
                Toets ()
                break
            else :
                print ("Fout, antwoord niet gedetecteerd. \nProbeer het nog een keer! \n\n")
                Toets()

    run_test(vragen)
Reply
#2
(Nov-04-2019, 11:53 AM)Luke_0162 Wrote: I got some problems
what problems did you get? Any traceback?
The as is now will not do anything. You just define 2 nested functions and a nested class
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
We get before a**2 = a * a the error that it can't assign the operator. Do you know how we could solve this problem?
Reply
#4
This is not possible:
a**2 = c-b
a**2 = a*a
Objects are always assigned to the left operand. In your case you want to assign a number to a**2.
To get rid of the square, use simple math:

import from math sqrt
a**2 = c-b | sqrt
a = sqrt(c-b)
a**2 = a*a | sqrt
a = a # ok, assign a to a
If you want to compare something of equality, use the == operator.
If you are working with floats, use math.isclose(a, b) for equality checks.

But it still does not make sense, to compare a**2 with a*a, because mathematically the solutions are identical.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Nov-04-2019, 04:33 PM)DeaD_EyE Wrote: a**2 = c-b | sqrt

Thanks for ur help, I got this now. The only issue I got now, is that working with variable 'a'(which u can try urself using the code) doesn't work.

def Test ():
    import math
    b = 1
    c = 5
    a = math.sqrt(c-b)
    class Questions:
        def __init__(self, question, answer, answer2):
            self.question = question
            self.answer = answer
            self.answer2 = answer2
 
 
   questions_list = [
   ["Solve the equation x**2 + 1 = 5. \n \nRestriction: you can not use spaces in your answer. \nNote your answer in the following form if necessery: x=.. v √x v counter/denominator v x**2.",
    ("x=" + str(a) + "vx=-" + str(a)), ("x=-" + str(a) + "vx=" + str(a))],
    ]
   questions = []
 
   for item in questions_list:
        questions.append(Questions(item[0],item[1],item[2]))
     
   def run_test(questions):
        for questions in questions :
            print(questions.question)
            answer = input("\nAnswer: ")
            if answer == ("x=" + str(a) + "vx=-" + str(a)):
                print("\nCorrect!\n")
                break
            if answer == ("x=-" + str(a) + "vx=" + str(a)):
                print("\nCorrect!\n")
                break
            if answer == ("x="+str(a)):
                print ("\nFalse, don’t forget that a negative x-value can also come out.\n\n")
                Test()
                break
            if answer == ("x=-"+str(a)):
                print ("\nFalse, don’t forget that a positive x-value can also come out.\n\n")
                Test()
                break
            if answer == ("x=√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=√2vx=-√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=-√2vx=√2") :
                print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
                Test()
                break
            if answer == ("x=√4vx=-√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=-√4vx=√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            if answer == ("x=-√4") :
                print ("\nFalse, you can work out your answer even further.\n\n")
                Test()
                break
            else :
                print ("nFalse, answer not detected. \nTry again! \n\n")
                Test()
 
    run_test(questions)

Problem explanation: When I start the programma now and when I type in as my answer: x=2vx=-2, I get 'False,..' while this actually is the right answer. This probably has something to do with the way variable 'a' is noted in the code, but I don't know how you should do it the right way.
Reply
#6
I'm confused that your answer is an equation with two equal signs. Can you explain?
Reply
#7
The = operator is the named assignment.
On the left side is the name, where you want to assign the object on the right side.
The left operand is known as Variable, but in Python this name is misleading.

a = 42
# the name a points now to the memory address of the integer object with the value 42
a = 43
# the name a points now to the memory address of the integer object with the value 43
b = a
# a == b
b = 13
# b == 13 and a == 43
The == is the operator just for equality check.

if True == False:
    print('Impossible')
The opposite of equal, is not equal.

if True != False:
    print('This is True')
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
This is my programme right now. It doesn't work because of this error: AttributeError: 'list' object has no attribute 'question'. Can someone help?

def Test ():
import math
b = 1
c = 5
a = math.sqrt(c-b)
class Questions:
def __init__(self, question, answer, answer2):
self.question = question
self.answer = answer
self.answer2 = answer2

questions_list =[
["Solve the equation x**2 + 1 = 5. \n \nRestriction: you can not use spaces in your answer. \nNote your answer in the following form if necessery: x=.. v √x v counter/denominator v x**2.",
("x=" + str(a) + "vx=-" + str(a)), ("x=-" + str(a) + "vx=" + str(a))],
]
questions = []

for item in questions_list:
questions.append(Questions(item[0],item[1],item[2]))

def run_test(questions):
for question in questions :
print(questions.question)
answer = input("\nAnswer: ")
if answer == question.answer or answer == question.answer2:
print("\nCorrect!\n")
break
if answer == ("x="+str(a)):
print ("\nFalse, don’t forget that a negative x-value can also come out.\n\n")
Test()
break
if answer == ("x=-"+str(a)):
print ("\nFalse, don’t forget that a positive x-value can also come out.\n\n")
Test()
break
if answer == ("x=√2") :
print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
Test()
break
if answer == ("x=√2vx=-√2") :
print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
Test()
break
if answer == ("x=-√2vx=√2") :
print ("\nFalse, you haven taken the root twice of 5 - 1 = 4.\n\n")
Test()
break
if answer == ("x=√4vx=-√4") :
print ("\nFalse, you can work out your answer even further.\n\n")
Test()
break
if answer == ("x=-√4vx=√4") :
print ("\nFalse, you can work out your answer even further.\n\n")
Test()
break
if answer == ("x=√4") :
print ("\nFalse, you can work out your answer even further.\n\n")
Test()
break
if answer == ("x=-√4") :
print ("\nFalse, you can work out your answer even further.\n\n")
Test()
break
else :
print ("nFalse, answer not detected. \nTry again! \n\n")
Test()

run_test(questions)
Reply
#9
Please post using the Python tags so we can see proper indentations. They matter. Also, please post the entire error message. That usually specifies a line number which can also be helpful.
Reply
#10
The error code:

Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
Toets()
File "/Users/luke/Documents/PWS/PWS 5.0.py", line 72, in Test
run_test(questions)
File "/Users/luke/Documents/PWS/PWS 5.0.py", line 27, in run_test
print(questions.question)
AttributeError: 'list' object has no attribute 'question'
Reply


Forum Jump:

User Panel Messages

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