I have just started learning python 2.7, and as every new learner i am still getting accustomed to the syntax that python uses. I tried to write this code:
name = raw_input('What is your name?\n')
print 'Hi, %s.' % (name)
I guess the output for the above program should be:
Hi, What is your name?
But i am getting the output as:
What is your name?
After pressing enter key, i get another output:
Hi, .
What is the problem with my code?
There is no problem, it does exactly what you tell it to.
raw_input prints the string argument (as a prompt), and reads input from the user until the first newline, returning the text it has read. This is exactly what happens (try typing some text before hitting Enter). Then the second line takes that text and puts it into the formatting string, printing the result.
The raw_input function is used to take input form the prompt. The string you passed in function will be printed on the prompt followed by your input which gets completed (in sense of python) with the pressing of 'enter key'.
After that the next line print 'Hi, %s.' % (name) will get printed showing the name entered by user in the prompt, which, I assume, is None in your case as you are pressing enter key without any input.
Related
I'm trying to execute the following code in Spyder 3.3.1 using Python 2.7.15. I'm a beginner.
text = str(input("You are lost in forest..."))
while text == "Right":
text = str(input("You are lost in forest..."))
print "You got out of the forest!!!"
When I run the code with integer value it works. For example the following piece of code:
text = input("You are lost in forest...")
while text == 1:
text = input("You are lost in forest...")
print "You got out of the forest!!!"
How can I make the input() work with a string value? Thank you for your help.
Use raw_input() instead of input():
value = raw_input("You are lost in forest...") # String input
value = int(raw_input("You are lost in forest...")) # Integer input
...
In python 2, raw_input() takes exactly what the user typed and passes it back as a string. input() tries to understand the data entered by the user.Hence expects a syntactically correct python statement.That's why you got an error when you enter Right. So you can enter "Right" as input to fix this error.
But it is better to use raw_input() instead of input().
In the "Learn Python the hard way" book in exercise 11 I found something like this:
print "How old are you?",
age = raw_input()
And the output is:
How old are you? 38
But when I add the comma in Python 2.7.13 it just prints the print statement and a new line begins.
It's a script where you need to have this code, not in the interactive console.
That is preferrably. If you still want to run it on the console, read here since I don't wanna copy and reword :D
If you run your script, your console will print the line:
How old are you?
You can than enter your age into the console ('38'), and your age is than saved into the variable 'age'. Try entering your age in the console and than printing 'age'.
Or try something like this:
print "How old are you?",
age = raw_input()
print ("I am " + age + " years old.")
In simple words I have a file which contains duplicate numbers. I want to write unique numbers from the 1st file into a 2nd file. I have opened the 1st file in 'r' mode and the 2nd file in 'a+' mode. But it looks like that nothing is appended in the 2nd file while the program is running which gives wrong output. Any one can help me how do I fix this problem.
Thank you in advance.
This is my code
#!/usr/bin/env python
fp1 = open('tweet_mention_id.txt','r')
for ids in fp1:
ids = ids.rstrip()
ids = int(ids)
print 'ids= ',ids
print ids + 1
fp2 = open('unique_mention_ids.txt','a+')
for user in fp2:
user = user.rstrip()
user = int(user)
print user + 1
print 'user= ',user
if ids != user:
print 'is unique',ids
fp2.write(str(ids) + '\n')
break
else:
print 'is already present',ids
fp2.close()
fp1.close()
If unique_mention_ids.txt is initially empty, then you will never enter your inner loop, and nothing will get written. You should use the inner loop to determine whether or not the id needs to be added, but then do the addition (if warranted) outside the inner loop.
Similar logic applies for a non-empty file, but for a different reason: when you open it for appending, the file pointer is at the end of the file, and trying to read behaves as if the file were empty. You can start at the beginning of the file by issuing a fp2.seek(0) statement before the inner loop.
Either way: as written, you will write a given id from the first file for every entry in the second that it doesn't match, as opposed to it not matching any (which, given the file name, sounds like what you want). Worse, in the second case above, you will be over writing whatever came after the id that didn't match.
When I run the below code it prints out a calendar for an entire year (which I don't want). I want it to write to file but it won't. It also returns the error message TypeError: expected a character buffer object. Also, casting it into a string doesn't work.
import calendar
cal = calendar.prcal(2015)
with open('yr2015.txt', 'w') as wf:
wf.write(cal)
As an example, the below code prints one month of a year and returns a string, but this isn't what I want
print calendar.month(2015, 4)
print type(calendar.month(2015, 4))
So when I run the below code I get the error message <type 'NoneType'>. It seems to me that it should be a string but obviously isn't. Any suggestions on how I can get a 12-month calendar into a text file?
print type(calendar.prcal(2015))
prcal doesn't return anything. Use cal = calendar.TextCalendar().formatyear(2015) Instead.
Your question is a bit confusing. However, you don't have to make python write to the file.
Let python write to stdout and redirect stdout to a file
python myCal.py > yr2015.txt
This should do the trick
That is because calendar.prcal() will only print the calendar of an year. And it wont return you any values. So this line in your code print type(calendar.prcal(2015)) will return none type error.
I Learn Python From "Learn Python the Hard way"
I don't know what is argv !!
(please explain argv with Example and text)
Question 2:
What is Different between raw_input & argv ?
argv stands for argument value and it represents the arguments passed to your program when it is launched from the command line.
For example, if your program is called example.py, and you run it like this:
$ example.py 'hello'
Then argv is hello.
raw_input is a way to prompt the user for some input. Basically, it will stop the program, display some text (as a prompt, but this is optional) and only continue when the user enters something. You can then store what the user entered.
If you have the following:
username = raw_input('Please enter your name: ')
print('Your name is: {}'.format(username))
Your program will run like this:
$ example.py
Please enter your name: Burhan
Your name is: Burhan