Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Conflict? Why?
#1
I have, in the same directory, created a file SecretPasswordFile.txt with a word placed inside as the password key.

Next, I created this file:
#! /usr/bin/env python
passwordFile = open('SecretPasswordFile.txt')
secretPassword = passwordFile.read()
print('Enter your password')
typedPassword = input()
if typedPassword == secretPassword:
print('Access granted')
if typedPassword == '12345':
print('That password is one that an idiot puts on their luggage.')
else:
print('Access denied')

I have checked most of the commands by typing them individually on the python console or IDLE.

But when I try to run this file, I get error msg:
python3.3 add.py
  File "add.py", line 7
    print('Access granted')
        ^
IndentationError: expected an indented block
Why am I receiving the above error messages?

For example,
$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> python3.3
Python 3.3.2+ (default, Jun 13 2013, 13:47:13) 
[GCC 4.8.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print ("hello")
hello
>>> print('hello')
hello
>>> print ('hello')
hello
>>> print("hello")
hello
>>> 
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply
#2
the big feature of Python is that is uses indenting of lines of code to know what level of blocking you meant. for example, after an if statement, the next line is indented to show that it is inside the if block. when a line is reached that is not indented, the if block is done. that line might then be an else statement and then the else block is indented until it isn't.

here is a small example with just one line in the if block and also just one line in the else block.

a = 4
if a > 6:
   print('a is greater than six')
else:
   print('a is less than six or equal to six')
print('a is',a)
change your code to have the indents and try again. i also recommend to use python3 if is installed on your computer.

i also recommend to upgrade your computer software and python. the latest version is 3.7.0.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#3
(Sep-28-2018, 05:26 AM)Skaperen Wrote: the big feature of Python is that is uses indenting of lines of code to know what level of blocking you meant. for example, after an if statement, the next line is indented to show that it is inside the if block. when a line is reached that is not indented, the if block is done. that line might then be an else statement and then the else block is indented until it isn't.
... ... ... ... ... ... ... ...
change your code to have the indents and try again.
That should have been the first sentence of the books. Instead, Al Swiegart speaks that first in page #39 with an MAYBE-MAYBE NOT " ... Lines of Python code can be grouped together in blocks. You can tell when a block begins and ends from the indentation of the lines of code ..."

Yes, used indentation and solved. Thank you.

(Sep-28-2018, 05:26 AM)Skaperen Wrote: i also recommend to use python3 if is installed on your computer.
Yes, I'm using Python3.3

(Sep-28-2018, 05:26 AM)Skaperen Wrote: i also recommend to upgrade your computer software and python. the latest version is 3.7.0.
Presently, I can't as I use an older flavour of Knoppix. 7.2.0

Al Swiegart's book was copyrighted on 2015. I checked the version here:
https://www.python.org/doc/versions/
Al couldn't have used a version later than 3.4.4
However, there is supposed to be a backward compatibility. However, I am not posting excuses. I shall try to have the latest versions installed, if they are standalone and doesn't crash my system.

Sorry, here: https://www.python.org/downloads/

Mekire Wrote:the code tags are [code][/code] not [icode][/icode]
I apologise. Sure, Sir. Will remember. Actually, different forums have only some BBcodes enabled. Which is why I used the tools of the edit toolbar above. I have to experiment for some time to remember.
But I am not justifying. I apologise again.

I have formatted according to suggestion. But there is one issue that remain. i am not getting the part, "That password is one that an idiot puts on their luggage."
Then I rearranged the code to the following and met with success.
#! /usr/bin/env python
# success
passwordFile = open('SecretPasswordFile.txt')
secretPassword = passwordFile.read()
print('Enter your password.')
typedPassword = input()
if typedPassword == '12345':
	print('That password is one that an idiot puts on their luggage.')
if typedPassword == secretPassword:
	print('Access granted')
else:
	print('Access denied')
Thank you, SkaperenDa Bishop and Mr. Mekire!
Python is far intuitive than c and even java, it appears.
Freedom is impossible to conceive.
Books that help:
Dale Carnegie's How To Win Friends And Influence People and Emilie Post's Etiquette In Society, In Business, In Politics, And At Home
Have a good day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Folium: Conflict with Font Awesome Kit jgomes_eu 0 1,219 Apr-23-2022, 03:18 PM
Last Post: jgomes_eu
  How to order the Cronjobs to avoid overlap or conflict sadhaonnisa 1 1,833 Oct-10-2020, 10:26 AM
Last Post: DeaD_EyE
  possible ctypes and numpy conflict? herbal_rage 0 3,145 Apr-16-2020, 11:35 AM
Last Post: herbal_rage
  Python 2.7 Import error. Directory and filename conflict petcoo00 2 2,380 Feb-02-2020, 08:46 PM
Last Post: snippsat
  PyInstaller and RStudio conflict rowdy 2 3,160 Jun-22-2019, 03:19 PM
Last Post: rowdy

Forum Jump:

User Panel Messages

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