0
Len, number, string, what did I miss
I am a TOTAL beginner, so sorry if my question is basic. I was trying to call a function inside a function. Basically using the len() to know if a number had more than 10 digit. Did not work. So I tested each part and here is what I am not understanding. I was trying to use the Len function to know if an number had more than 1 digit. If I write : x = 90 y = 4 d = x*y*x e = "d" print(len(e)) print(d) It print : 1 and 32400. e is 'd' so the Len shoud be counting 5 elements. And if I type : x = 90 y = 4 d = x*y*x e = "32400" print(len(e)) This time e is five . What I am missing ? writing e = "d" should make e string no ? This drove me crazy for more than one hour.
10 ответов
+ 5
d=x*y*x #32400
e=str(d) #"32400"
+ 2
x=90
y=4
d=x*y*x
e=str(d)
print (len(e))
You just have to the function of d to str.
This will work
+ 2
What you did for your first code was set variable 'e' to hold the value of a string, '"d"'.
>>>e = "d"
This told Python, when you wanted to 'print(len(e))', to display the string '"d"' length, which is 1.
>>>print(len(e))
1
Then, when you printed the variable 'd', Python printed the result of 90*4*90, which is integer 32400.
>>>d = x*y*x
>>>print(d)
32400
Other responses show you the right code to get your desired results:
>>>e = str(d)
What this line does is convert the integer value of variable 'd' into a string and assign that value to variable 'e'. When you print the length of 'e' you will get 5.
>>>print(len(e))
5
+ 1
Your e variable contains just letter d.
e=str(d)
Makes the d variable a string.
0
Thanks !
But why writing e = 'd' was not working ? Doing this does not transform the element in a string ?
Well I know it doesn't because other wise my code would have work.
I am trying to understand why
e = 32400
and
e = 'd' when d = 32400
are not behaving the same way with the Len() function.
0
Thanks for all the input but the question was more on the basic
why :
e = 'd' give me 1 with len(), but a 5 digit number when I use print?
I am just trying to understand how value behave inside the ' ' elements
0
Thanks that's what I did not understand.
I will test different variations this evening.
0
You're welcome. It's good practice as a beginner to do some traceback. What I mean is get a piece of paper and pencil and run through your program as if you were to execute those sets of instructions yourself whenever you get an error. When you look to writing out your program, you should practice writing algorithms or a flow chart to better keep track of how you want your program to work. I hope you found this useful for future projects.
0
the problem is in this part of code:
e='d'
the python enterpreter treat this code as e variable is assigned to d character not as d variable equal.
youcan replace 'd' with str(d).
so it convert d variable to string and you can see result as you can see in your second code
0
Thanks for all the input.
I am doing exercice now :-)
One was to write a code asking for 2 values, and printing back the product of the two if it was inferior to 1000, or other wise the sum.
The solution given was.
def multiplication_or_sum(num1, num2):
product = num1 *num2
if(product < 1000):
return product
else:
return num1 +num2
number1 = int(input("Enter first number "))
number2 = int(input("Enter second number"))
result = multiplication_or_sum(number1, number2)
print("The result is", result)
My solution
x = int(input('first value:'))
y = int(input('second value:'))
if x*y <= 1000:
print(" the resuls is:", x*y)
else:
print("the results is:", x+y)
Shorter but only because looking at the given solution I realize that I could read the input directly as a number using the
x = int(input('first value:'))
Structure.
My first iteration was asking for input then converting it to a numerical value with another line of code.