Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Help me with a simple print syntax error.

Hey guys,

I am currently learning Python and I am a complete newbie so please do excuse me if this is the most simple mistake ever to you.
Would someone please tell me what is wrong with my code and why I get a syntax error when I try to run it ?
The code can be found below:


print("Hello, what is your name?")
myname = input("Enter your name")

if myname == "Oli"
print("Access granted.")

elif myname == "Adam"
print("You are on the wrong computer.")

else print("Access denied.")



Thank you guys, I know this may be incredibly easy for some of you but I am currently learning so please try refrain from laughing at me lol.

Thanks :)

-Oli
You're probably on 2.7.
Upgrade your python to 3.4
(Jul 23, 2017 04:28 PM)Charger Wrote:  You're probably on 2.7.
Upgrade your python to 3.4

I am using the latest version of Python
You need a colon after the conditionals.

PHP Code

<?php 
print("Hello, what is your name?")
myname = input("Enter your name")

if 
myname == "Oli":
    print(
"Access granted.")

elif myname == "Adam":
    print(
"You are on the wrong computer.")

else: 
    print(
"Access denied.") 
[Image: npafFhp.gif]
Code
print("Hello, what is your name?")
myname = input("Enter your name")

if myname == "Oli":
  print("Access granted.")
elif myname == "Adam":
  print("You are on the wrong computer.")
else:
  print("Access denied.")
Code
print("Hello, what is your name?")
myname = input("Enter your name")

if myname == "Oli":
  print("Access granted.")

elif myname == "Adam":
  print("You are on the wrong computer.")

else:
  print("Access denied.")

Fixed it up for you. You were missing the colons after the if statements and the indents before the results.

EDIT: I was beat to it, rip.
[Image: VFmqv4d.gif]
(Jul 23, 2017 04:30 PM)Retribute Wrote:  You need a colon after the conditionals.

PHP Code

<?php 
print("Hello, what is your name?")
myname = input("Enter your name")

if 
myname == "Oli":
    print(
"Access granted.")

elif myname == "Adam":
    print(
"You are on the wrong computer.")

else: 
    print(
"Access denied.") 

Thank you so much, got it working now :) I know it was a newbie mistake but thanks anyway haha
OP even thou your just learning the language try to practice to keep the code clean and try to always add comments to your work so if you look back at it in say 10 or 20 year you know what you were doing .

PHP Code

<?php 
myname 
= input("Hello, what is your name? : ") #myname is a variable which holds a user's inputted name.

if myname == "Oli" :#conditional statement which checks to see if the user inputted Oli, if it is met then print the message below
    
print("Access granted.")
 
elif myname == "Adam":#conditional statement which checks to see if the user inputted Adam, if it is met then print the message below
    
print("You are on the wrong computer.")

else:  
#if the conditions above are not met then print Access denied
    
print("Access denied.") 
Knowledge is Power - Be Powerful!!!!!


LIFE IS A CHALLENGE SO ACCEPT IT!
(Jul 24, 2017 03:50 AM)koolkid Wrote:  OP even thou your just learning the language try to practice to keep the code clean and try to always add comments to your work so if you look back at it in say 10 or 20 year you know what you were doing .

PHP Code

<?php 
myname 
= input("Hello, what is your name? : ") #myname is a variable which holds a user's inputted name.

if myname == "Oli" :#conditional statement which checks to see if the user inputted Oli, if it is met then print the message below
    
print("Access granted.")
 
elif myname == "Adam":#conditional statement which checks to see if the user inputted Adam, if it is met then print the message below
    
print("You are on the wrong computer.")

else:  
#if the conditions above are not met then print Access denied
    
print("Access denied.") 

I wouldn't recommend commenting your code like this. For simple scripts like what you're creating, you should be able to read it fluently without any comments. If you're just starting off, sure, but when you're larger scripts, commenting should only be done where you'll think you need it.

Don't narrate your script, it's not a book.
Skype messages are NOT valid evidence, and here's why.
You was missing a colon after your final conditonal.
Perhaps consider sanitizing input and comparing as case-insensitive by applying the .lower() or .upper() method on strings in your conditionals.
Thank you to everyone who responded, I have taken all of your replies into consideration :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by simmerJul 30, 2021 10:08 AM 7 230 Jul 30, 2021 10:08 AM
Last Post: simmer
[C] by oh9odSep 16, 2020 04:24 PM 5 183 Sep 16, 2020 04:24 PM
Last Post: omboml
[C] by ShallowMEFeb 17, 2020 07:08 PM 20 522 Feb 17, 2020 07:08 PM
Last Post: 0xDEAD10CC