conversion of a variable in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

conversion of a variable in python

Hello :) So i've got a question who keeps bothering me : you know how in Python we can easily convert a string in integer by doing : >>>> var = new_type(type) for instance, number= int("2") will return 2 as an integer. BUT i've been trying a piece of code like this : >>>>> demand=input(": ") base=int(len(demand)) i=0 temp="0" nb_temp=0 temp+=demand[i] i+=1 nb_temp=int(temp) <<<<<< So, technically, (the code isn't complete here), let's imagine the user input is "15+21" The idea here is to stock "1", then "5" in temp, so the variable is equal to the string "15". then i want to fill another variable with the integer 15. SO i try to convert it by the code above : nb_temp=int(temp), since temp is worth "15", and nb_temp=int("15") DOES work. SO i don't get why i can't make it work with my variable o.o Little edit, simplification : i'm trying to do ******************* a=input(": ") b=0 c="0" while(b<=len(a)): c+=a[b] d=float(c) print(d) ************ wich is suppose to give the same result as ***************** a=input(": ") b=float(a) print(b) **************** Those two codes are supposed to works just as well, except the first one is way better to use, but seems not to work --'

1st Mar 2018, 3:31 PM
Clement combes
Clement combes - avatar
2 Answers
+ 1
indeed, I wrote my second example too quickly, thanks for pointing it out :) but my initial difficulty stays the same, I don't get my it doesn't work ;-(
1st Mar 2018, 8:09 PM
Clement combes
Clement combes - avatar
0
In your first example b is never incremented, so the while loop will never end. while(b<=len(a)):
1st Mar 2018, 6:36 PM
Louis
Louis - avatar