Write an expression whose value is the result of converting the str value associated with s to an int value.

I tried typing int('s') in python2.7, and I got an error.

Recommended Answers

All 4 Replies

come on now, obviously you have to assign the variable to a value...

>>> s=10
>>> 
>>> int(s)
10
>>> print(s)
10
>>> s+3
13

but that only answers part of it, first you have to assign s to a string value, HINT: this is done by surrounding the value with '' or "". note that above s was associated with an integer value to begin with, not a string value. Also, you should post what you come up with as a courtesy to show that you put in effort, don't forget to mark threads solved either.

The answer I attempted was int('s'), which does not work. Since int('42') gives 42. Why does int('s') not work since s is associated with an integer value. I don't understand what you are saying at all. Can you please tell me how to write the expression please?

When you use 's' it`s a string s,not variable s.

>>> int('s')
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 's'
>>> s = '42'
>>> type(s)
<type 'str'>
>>> x = int(s)
>>> x
42
>>> type(x)
<type 'int'>

@pyguy62 when you do s = 10,then s is a integer.
No need to convert to integer.

ok, I solved it! I just try int(s) and it works!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.