![]() What does it mean to multiply two strings in the first place? However, it makes sense in a way to specify multiplying a string by a number - just repeat that string that number of times. In terms of multiplication, the idea of multiplyling two strings together is not well-defined. The same works for multiplication: print ( 4 * "test" )īut division and subtraction do not work like this. What do I mean by that? I mean, if I want to combine ( concatenate is the computer science word for this) strings, all I need to do is add them: print ( "Were" "wolf" ) print ( "Door" "man" ) print ( "4" "chan" ) print ( str ( 4 ) "chan" ) (Note, you can also turn integers into strings exactly in the opposite way, In both cases, age will hold a variable that is an integer, and now you can do math with it. (or, if you want to be more compact with your code) age = int ( input ( "Enter your age: " )) Here is what this looks like: age = input ( "Enter your age: " ) age = int ( age ) (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this for now don’t worry about that problem). You can turn the string into an integer with the function int(). Let’s say you are 100% positive that the user entered a number. import math def mydist2points(xypoints, xy): Returns an array of distances between. What can you do with it?įirst: Make the string into a number. Alternatively, you could use the index to get each character. What you get from the input() function is a string. Only after the user presses ENTER does the program continue. What happens at the end of input() is that it waits for the user to type something and press ENTER. What this will print in the terminal (or the shell, whatever you are running Python in) will be: > Give me your name : Michele Your name is Michele Remember that the result you get from the user will be a string, even if they enter a number.įor example, name = input ( "Give me your name: " ) print ( "Your name is " name ) Store the result in a variable, and use it to your heart’s content. ![]() To get user input in Python (3), the command you use is input(). Exercise 38 shows another way of manipulating and displaying strings ( Hint: the string "\n is the same as pressing the ENTER button) Print out that many copies of the previous message on separate lines.( Hint: order of operations exists in Python) Add on to the previous program by asking the user for another number and printing out that many copies of the previous message.If you want to do this in a generic way, see exercise 39. A square root sign should denote the operation of taking a square. You’ll also get an overview of Python’s built-in functions. By the end of this tutorial, you’ll be familiar with what objects of these types look like, and how to represent them. Note: for this exercise, the expectation is that you explicitly write out the year (and therefore be out of date the next year). I find it a very good thing that Python doesnt accept arithmetic operations as variables. Here’s what you’ll learn in this tutorial: You’ll learn about several basic numeric, string, and Boolean types that are built into Python. Print out a message addressed to them that tells them the year that they will turn 100 years old. Create a program that asks the user to enter their name and their age.
0 Comments
Leave a Reply. |