Computer Science, asked by Dikshayaduwanshi003, 2 months ago

single
15, one) Python statement :
(a) Tell the user to Enter your name, then read in, and assign to a variable, a string
typed in by the user.​

Answers

Answered by antonypravin412
3

Answer:

indexnext |previous |Hands-on Python Tutorial » 1. Beginning With Python »

1.10. Input and Output

1.10.1. The input Function

The hello program of The Classic First Program always does the same thing. This is not very interesting. Programs are only going to be reused if they can act on a variety of data. One way to get data is directly from the user. Modify the hello.py program as follows in the editor, and save it with File ‣ Save As....`, using the name hello_you.py.

person = input('Enter your name: ')

print('Hello', person)

Run the program. In the Shell you should see

Enter your name:

Make sure the typing cursor is in the Shell window, at the end of this line. Then follow the instruction (and press Enter). After you type your response, you can see that the program has taken in the line you typed. That is what the built-in function input does: First it prints the string you give as a parameter (in this case 'Enter your name: '), and then it waits for a line to be typed in, and returns the string of characters you typed. In the hello_you.py program this value is assigned to the variable person, for use later.

The parameter inside the parentheses after input is important. It is a prompt, prompting you that keyboard input is expected at that point, and hopefully indicating what is being requested. Without the prompt, the user would not know what was happening, and the computer would just sit there waiting!

Open the example program, interview.py. Before running it (with any made-up data), see if you can figure out what it will do:

'''Illustrate input and print.'''

applicant = input("Enter the applicant's name: ")

interviewer = input("Enter the interviewer's name: ")

time = input("Enter the appointment time: ")

print(interviewer, "will interview", applicant, "at", time)

The statements are executed in the order they appear in the text of the program: sequentially. This is the simplest way for the execution of the program to flow. You will see instructions later that alter that natural flow.

If we want to reload and modify the hello_you.py program to put an exclamation point at the end, you could try:

person = input('Enter your name: ')

print('Hello', person, '!')

Run it and you see that it is not spaced right. There should be no space after the person’s name, but the default behavior of the print function is to have each field printed separated by a space. There are several ways to fix this. You should know one. Think about it before going on to the next section. Hint: [1]

[1] The + operation on strings adds no extra space.

1.10.2. Print with Keyword Parameter sep

One way to put punctuation but no space after the person in hello_you.py is to use the plus operator, +. Another approach is to change the default separator between fields in the print function. This will introduce a new syntax feature, keyword parameters. The print function has a keyword parameter named sep. If you leave it out of a call to print, as we have so far, it is set equal to a space by default. If you add a final field, sep='', in the print function in hello_you.py, you get the following example file, hello_you2.py:

'''Hello to you! Illustrates sep with empty string in print.

'''

person = input('Enter your name: ')

print('Hello ', person, '!', sep='')

Try the program.

Keyword parameters must be listed at the end of the parameter list. They have the keyword name, followed by an equal sign, before the value being given. We will see another example shortly.

It is a standard Python convention that when giving a keyword and value, the equal sign has no space on either side. (This is the only place you are not encouraged to set off an equal sign with spaces.)

1.10.3. Numbers and Strings of Digits

Consider the following problem: Prompt the user for two numbers, and then print out a sentence stating the sum. For instance if the user entered 2 and 3, you would print ‘The sum of 2 and 3 is 5.’

You might imagine a solution like the example file addition1.py, shown below. There is a problem. Can you figure it out before you try running it? Hint: [2]

'''Error in addition from input.'''

x = input("Enter a number: ")

y = input("Enter a second number: ")

print('The sum of ', x, ' and ', y, ' is ', x+y, '.', sep='') #error

End up running it in any case.

Answered by deekondasanjay48
0

Answer:

hi friend how are you

Explanation:

have u deleted ur ins.ta account

Similar questions