Codecademy, Python code won't work - python

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

Related

[Python]-Can not determine variable type by assigning a value to it using a input() func, why?

I’m assigning a numerical value to a variable called code using the input() function in the terminal. Below are my lines of code:
code = input("Please enter your code:\n")
print(code)
print("\n")
print(type(code))
my question though is that, why after doing so I wouldn’t be able to determine the type of my variable called code, using this line of code print(type(code)). My interpreter in VS Code would give me back this error:
Please enter your code:
2
Unable to find thread for evaluation.
Is there something wrong with my debugger settings or Debug Console? or something that I’m missing? I’m pretty new to all of this, so please explain in simple terms. Thanks a lot.
PS. I've set my console to "internalConsole", maybe that's what is causing this !?

Clearing Print in Python

So I'm pretty new to both coding and this website, so please bear with me if this is stupid:
I'm working on a personal project and would like to find a way to clear "print()" statements in python 3.6. For example:
print("The user would see this text.")
but if I continue
print("The user would see this text.")
print("They would also see this text.")
Is there a way to make it so a user would only see the second print statement?
I have seen "os.system('cls')" and "os.system('clear')" recommended, but I get these errors for each:
os.system('cls')
resulting in
sh: 1: cls: not found
and
os.system('clear')
resulting in
TERM environment variable not set.
Obviously I'm missing something, but if you know what it'd be much appreciated. If you know of another way to do what I'm thinking, that would also be awesome. Thank you for taking the time to read this, and thanks for any help.
Edit: I'm using Repl.it as my IDE. Could this be an issue with that site specifically?
Edit: Downloaded a new IDE to check, and the reply worked. If you are new and using Repl.it, be aware that some code does not function properly.
The method that I've used in the past to 'reprint' something on an existing line is to make use of the standard output directly, coupled with a carriage return to bring the printed statement's cursor back to the start of the line (\r = carriage return), instead of relying on the print function.
In pseudocode:
# Send what you want to print initially to standard output, with a carriage return appended to the front of it.
# Flush the contents of standard output.
# Send the second thing you want to print to standard output.
A working example in Python:
import sys
sys.stdout.write('\rThe user would see this text')
sys.stdout.flush()
sys.stdout.write('\rThe user would also see this text')
Edit
Figured I'd add an example where you can actually see the code working, since the working example above is going to execute so quickly that you'll never see the original line. The below code incorporates a sleep so that you can see it print the first line, wait, then reprint the line using the second string:
import sys
from time import sleep
sys.stdout.write('\rThe user would see this text')
sys.stdout.flush()
sleep(2)
sys.stdout.write('\rThe user would also see this text')

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

Python IDLE with Python 3.5.2 "crashing"

Okay, so let me just say beforehand: I am new to Python. I was just experimenting with IDLE and then I had this weird "crash". I put "crash" inside speech marks because I'm not sure if it qualifies as a crash, as rather than the program just crashing the way a normal program would in Windows, it still runs, but whenever I press enter and try and get it to accept new text it doesn't do anything. E.g. if you try and type "print('a')" and then hit enter it just goes to the next line (and doesn't print 'a'). I tried to make a simple function which converted an integer to a string where each character in the string was either a '1' or a '0', forming the binary number representing said (unsigned) integer.
>>> def int_to_str(int_in):
str_out=''
bit_val=1<<int_in.bit_length()
while(int_in>0):
if(int_in>bit_val):
str_out+='1'
int_in-=bit_val
else:
str_out+='0'
bit_val>>=1
return str_out
>>> print('a')
print('c')
Basically, it becomes completely unresponsive to my input, and allows me to edit/change "print('a')" even though I shouldn't be able to if it had actually "accepted" my input. Why is this? What have I done wrong/messed up?
Also, I made sure it isn't something else I was previously messing around with by closing the shell and opening it again and only putting in said code for the "int_to_string" function, and I haven't changed any settings or imported any modules before hand or anything like that (in case it matters).
EDIT: I tried reinstalling, and that helped a bit in that I can now do other stuff fine, but the moment I try to use the "str_to_int()" function, it has this same weird behaviour of not accepting/interpreting any more user input.
Your while loop never terminates, you need to re-arrange your logic. Printing variables can be an effective debugging tool - like this:
>>> def int_to_str(int_in):
str_out=''
bit_val=1<<int_in.bit_length()
while(int_in>0):
print(int_in, bit_val)
if(int_in>bit_val):
str_out+='1'
int_in-=bit_val
else:
str_out+='0'
bit_val>>=1
return str_out
If your program seems to be going on too long you can stop it with ctrl-c.

time.localtime() question beginner 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.

Categories

Resources