How would this be done? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How would this be done?

Write a line of code that *converts x to be an int and *stores it back into x

13th Mar 2018, 2:54 AM
Robbie Jones
Robbie Jones - avatar
11 Answers
+ 4
Well, if you are converting x to an integer, than you don't really have to "store it back" into x since it is already a variable and has been declared as an integer. You can encapsulate the variable in the int() function, and then it will be an integer. Keep in mind, strings can't be integers for obvious reasons.
13th Mar 2018, 2:58 AM
Dread
Dread - avatar
+ 3
Here is a code snippet that does exactly that: x = 15.4 print(x) x = int(x) print(x) x is declared as a float, and is printed to the screen; then x is re-declared as int(x), functionally turning it into an integer and rounds it down to 15; then the new value of x is printed.
13th Mar 2018, 3:09 AM
Dread
Dread - avatar
+ 2
A float to an integer? Just type : x = 15.4 (or whatever) print(int(x))
13th Mar 2018, 3:02 AM
Dread
Dread - avatar
+ 1
Sorry, I meant X was a sting in my reply.
13th Mar 2018, 3:03 AM
Robbie Jones
Robbie Jones - avatar
+ 1
Wouldn’t you think your answer here is probably what he’s looking for? I know it’s simple question. I’m just still learning. x = 15.4 (or whatever) print(int(x))
13th Mar 2018, 3:06 AM
Robbie Jones
Robbie Jones - avatar
+ 1
He didn’t give us one, which was why I was confused I guess. The problem he gave us is exactly what I started this thread with. Write a line of code that *converts x to be an int and *stores it back into x
13th Mar 2018, 3:08 AM
Robbie Jones
Robbie Jones - avatar
0
First off, thanks for answering so quickly. This is my professors question and he wants us to type the code for it. I’m not understanding the question because we weren’t given an integer unless I can just make one up. And how would I convert x if it’s just a string?
13th Mar 2018, 3:01 AM
Robbie Jones
Robbie Jones - avatar
0
A string to an integer? I guess you could turn the amount of characters into an integer.
13th Mar 2018, 3:04 AM
Dread
Dread - avatar
0
What is the string?
13th Mar 2018, 3:06 AM
Dread
Dread - avatar
0
What is the value of x? The one you were given.
13th Mar 2018, 3:06 AM
Dread
Dread - avatar
0
if your x is string and want to store it as int just x = int(x) you cant pass non numeral strings like "abcd" into int function.
13th Mar 2018, 3:15 PM
Markus Kaleton
Markus Kaleton - avatar