time.localtime() question beginner python - python

I'm having a syntax error on the second line of this code, I'm trying to make a counter with the winsound beep.
I think the problem is with the format() part, but i get a highlighted =, equals sign when i try to run the program. syntax error
def print_time(secs):
print('{0}:{1:02}'.format(secs//60,secs%60),end=' ')
print("left to wait...")
This is my second week programming, very basic understanding of comp sci or of languages.
This looks like a wonderful site to learn from.
If the part of the code I wrote looks fine, i can post the rest of it up as well to help find the problem.

It sounds like you're reading documentation for Python 3.x, but running Python 2.x. Try this instead:
def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
Also, divmod().

def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
The above code should work fine.
Python 3+ treats 'print' as a function and hence introduces end=' ' to suppress newline. But, in earlier versions of python it was done by appending ,(comma) to the print statement. See this link for what's new in Python 3+.
Apparently, your Python environment is 2.x and hence you are seeing the error.

Related

(Python 2.7) Syntax error when trying to append string to list

Heyo! I'm editing a script I found online that is written in Python 2.7. I've only ever coded in 3.8 so I'm not entirely familiar with the changes. Also still a beginner in Python 3.8
I've added some code to append a string to a list, but it throws me this syntax error whenever I try to run the script:
Here's the snippet of the code it refers to:
for r in range(0,self.__length):
for c in range(0,self.__width):
if self.__map[r][c] in (WALL,PERM_WALL):
print '#'
global mapdatalist.append("#")
else:
print '.'
global mapdatalist.append(".")
It's referring to the mapdatalist.append("#") bit.
I've read the part on list functions here, and from what I'm reading I'm doing it right.
Anyone who is more experienced with this can help me out? Thanks!
You cannot combine a global declaration with an expression like that. Use global mapdatalist once, at the beginning of the function, and then use mapdatalist normally (e.g. mapdatalist.append("#")). (This is the same whichever version of Python you're using.)
Assuming you want to append a string to the global variable mapdatalist,
you should declare the global variable before using the variable.
Try this:
global mapdatalist
for r in range(0,self.__length):
for c in range(0,self.__width):
if self.__map[r][c] in (WALL,PERM_WALL):
print '#'
mapdatalist.append("#")
else:
print '.'
mapdatalist.append(".")

pycharm syntax check error for python 3 print end=" " but still works fine

I am using Pycharm 2018.3.4. When python 3 is used, it indicates error for the end="" in the print function.
However, the code could still be run without any problem. So how to remove the red line for syntax check?
(I feel pycharm gets more popularity in stackoverflow than superuser, so I asked the question here)
Check your Interpreter's version is python 3.x in the right bottom of the pycharm, weather you are 3.x, You could try to retry the "InterpreterSetting" , this buttom.
I am not English speaker, I was left a comment by my language, but it was deleted by someone.
enter image description here

Python 2.7.13 - print command issue

I am absolutely new to python programming. i have written the below piece of code:
'''Lets us just learn python'''
print ("Hey God Help Me !!!")
name = raw_input("Enter Name:")
print name
While i run the code it asks for the input as intended however it doesn't print anything and looks like going in a infinite loop. i have used the same code in codeacademy system and it works. can someone help me understand what may be the issue?
i have tried using other functions and it works. Even tried manually entering the value in the code and that too works. However when ever i try to take a input from the user, this doesn't seem to work.
Well, after you changed your problem description to one, that I recommended initially:
print ("Hey God Help Me !!!")
name = raw_input("Enter Name:")
print name
then you need to assure you're using python 2.7 for 2 options of print statement -
print name and print(name) - to work.
Just in case you're using python 3x, please modify print name to print(name)
Hello fri3nd!
Welcome to party, I envy you! Not that I feel I've reached any plateau but that's the nature of the IT sector... You stay compliant to the current standard or your behind you... Anyhow firstly..
You say your on python 2.7? Youll quickly learn that firstly I assure you is that in 2.7 and 3.0+ you have different method of having to write your actual code... and the first ever code will have a printt lets take that for example...
Python 2.7
print 'HEY TH3R3'
Python3.0+
print('SEE THE DIFFERENCE?')
So when your running the code you provided... your mixing and matching which ... just cant be done,... So what your code would turnout to be properly
Python3.0+
name = input("gHey its... not god lol... whats your name? \n:")
greeting = "Oh! hi {}".format(name)
print(greeting)
I wont toch the 2.7 syntax but aside from the fact that we need to get you some quality tutorials lol... whats more important here is thatyou get the :logic of the language build... So much awesomeness awaits you. You ever need some help drop me a message but you have to put in that effort son lol.. but Ill give you a pass and place most of the blame oh bad source for the beging of the trip but yeah... google is your friend

Codecademy, Python code won't work

I have started programming in Python at Codecademy. So far I think that I've learn some basic programming skill and I'm really existed to try something more difficult.
On a project I've been working I keep getting this error
My code looks like this:
print "Welcome to the English to Pig Latin translator!"
original = raw_input("What's your name?")
def function():
if len(original) > 0:
print original
else:
print "empty"
The task is to see if the string is empty cause I will be using this piece of code later on.
When I press the 'Save & Submit Code' it prompts with my question and I type my answer in to the editor, and then nothing happens.
The message I get is: "The original variable ("Nicolai") had more than 0 characters but did not get printed."
Can anybody tell me what I'm doing wrong here?
It doesn't look like you're ever calling the function, only defining it. Try inserting:
function()
at the end. (also please name it more descriptively than "function")
Looking at the exercise, it doesn't want you to create a function. Simply remove the def function(): (and don't forget to un-indent your code!)
In fact, learning functions is after the PygLatin course :D

Understanding Global Names and Python2 and 3

As a newbie to Python, I'm kind of learning some of the differences between Python2 and 3. In working through the Python course, it seems that there are some things that need to be changed in the code to make it work in 3. Here's the code;
def clinic():
print "In this space goes the greeting"
print "Choose left or right"
answer = raw_input("Type left or right and hit 'Enter'.")
if answer == "LEFT" or answer == "Left" or answer == "left":
print "Here is test answer if you chose Left."
elif answer == "RIGHT" or answer == "Right" or answer == "right":
print "Here is the test answer if you chose Right!"
else:
print "You didn't make a valid choice, please try again."
clinic()
clinic()
To make this work in Python 3, the print syntax needs to be changed (add parens), but another issue that comes up is the error "NameError: global name 'raw_input' is not defined". I've seen this issue come up often in my learning. It doesn't seem to come up when I run it in Python2, but in 3 it seems to need it declared as a global. However, when I add "global raw_input" to the function, it doesn't seem to work (in other cases, it worked everytime I did it.) Can someone tell me what I'm doing wrong? Also, I've heard that declaring globals is a bad habit to get into when not necessary, so what's the best way to handle them?
raw_input() has been renamed in Python 3, use input() instead (and the old Python 2 input() was removed). See PEP 3111.
See What's new in Python 3.0 for an exhaustive overview. There is also the Dive into Python 3 overview.
Amending Martijn's answer, here's a general trick you can do for these kind of small incompatibilities:
try:
input_func = raw_input
except NameError:
raw_input = input
Afterwards you can just use raw_input in your script with both Py2 and Py3. Similar things might be required for the unicode, and byte types.
Since you indicated you're interested in migration from >=Py2.7 to a Py3, you should know that Python 2.7 was mostly a Python 2.6 with lots of Py3 stuff backported.
So, while the print function technically still is a statement in Py2.7, and a function in Py3, the Py2.7 print does accept tuples. And that makes some of the Py3 syntax work in Py2.7. In short, you can just use parantheses:
print("Here is the test answer if you chose Right!")
To print an empty line, the best method working in both versions would be
print("")
To print without adding a newline by default I'm resorting back to write(), e.g.:
import sys
sys.stdout.write("no newline here")
sys.stdout.write(" -- line continued here, and ends now.\n")
On the other hand, for lots of Py3 stuff, you can actually enable the full Py3 syntax in Py2.7 by importing things from the future:
from __future__ import print_function
Then you don't need to switch between write() and print().
In real applications it all depends on if and how you have to interact with other people's code (packages, other developers in your team, code publishing requirements) and what's your roadmap for Python version changes.

Categories

Resources