Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem in a python2 file
#11
post your code in python tags as well as full traceback it produce in error tags.
you have posted so many different snippets it's not clear which one you refer to.
if it is the one in post #4 note that your traceback in the same post does not reflect the code (i.e. that is not traceback produced from this code)
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
#12
First file "solver.py":
import click
import math
print("solver.py  in python2.7")

class Solver:
    def demo(self):
        print("Resolve second degree equation. Enter the coefficients(numbers): a,b,c")
        text = raw_input(' Are you ready: y/n ? ')
        print('you entered:  ', text)
        text = "y"
        while (text == 'y'):
            # while True:
            a = float(input("a "))
            b = float(input("b "))
            c = float(input("c "))
            d = b ** 2 - 4 * a * c
            if d >= 0:
                disc = math.sqrt(d)
                root1 = (-b + disc) / (2 * a)
                root2 = (-b - disc) / (2 * a)
                print(root1, root2)
            else:
                print('This equation has no roots')

            print('do you want to begin again for another equation ?  ')
            #text = raw_input(' y/n  ?   ')
            #print("you entered: ", text)
            print('sylas')
			click.echo('Do you agree ? y/n ', nl=False)
			text= click.getchar()
			click.echo() 
			if text == b'y':
				click.echo('We wiil continue')
			elif text == b'n':
				click.echo('Abort!')
				exit()
			else:
				click.echo('Invalid input ! ')
				exit()
			

Solver().demo()
                

	
Second file "about click"
#python2
import click

click.echo('Do you agree ? [y/n] ', nl=False)
c = click.getchar()
click.echo()
if c == b'y':
    click.echo('We will go on')
elif c == b'n':
    click.echo('Abort!')
    exit()
else:
    click.echo('Invalid input ! ')
    exit()
    
    
print("sylas")
The error message:
Error:
File "solver.py", line 29 click.echo('Do you agree ? y/n ', nl=False) ^ IndentationError: unexpected indent
Reply
#13
first - this code snippet works for me. No IndentationError
second - the only problem is from using input (instead of raw_input if this is going to be python2 code) and if I enter something different from numeric string for a, b or c
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
#14
Did you try the files on python2 ? My problem is on python2 since I cannot use python3 because python3 cannot import click. Thanks
Reply
#15
(Aug-08-2018, 07:39 AM)sylas Wrote: Did you try the files on python2 ? My problem is on python2 since I cannot use python3 because python3 cannot import click.
yes, python2. that is why I say I have problem because you use input and not raw_input.
and as already explained to you several times - install click for python3 if you want to run it with python3
C:\>C:/Python27/python.exe c:/solver.py
solver.py  in python2.7
Resolve second degree equation. Enter the coefficients(numbers): a,b,c
 Are you ready: y/n ? y
('you entered:  ', 'y')
a 1
b 6
c 9
(-3.0, -3.0)
do you want to begin again for another equation ?
sylas
Do you agree ? y/n
We wiil continue
a s
Traceback (most recent call last):
  File "c:/solver.py", line 42, in <module>
    Solver().demo()
  File "c:/solver.py", line 13, in demo
    a = float(input("a "))
  File "<string>", line 1, in <module>
NameError: name 's' is not defined
Also note that it doesn't matter which version I use - if there is indentation error it will be the same in python2 and python3
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
#16
Fantastic !! I copied the solver.py , just above, and deleted the older, when pasted it works fine, on python2. There are mysteries.
About python3, I already installed it with "sudo pip3 install click" but unfortunately it does not work. It tells he cannot import "main". One problem creates a second problem. Not easy !!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python3 decoding problem but python2 OK mesbah 0 1,802 Nov-30-2019, 04:42 PM
Last Post: mesbah
  Trying to run a python2 script dagamer1991 3 2,536 Aug-12-2019, 12:33 PM
Last Post: buran
  python2.7 executables thus the system python2.7 was erroring utility.execute()? vivekm 1 1,756 May-20-2019, 11:24 AM
Last Post: vivekm
  Python2 is not supported Skaperen 2 2,155 Mar-01-2019, 07:50 PM
Last Post: stranac
  Posting zip file on HTTP network using Python2.7 deepti92 1 6,785 May-15-2018, 12:41 PM
Last Post: deepti92

Forum Jump:

User Panel Messages

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