0

How to improve my “while” function

str = input() print(len(str)) x=0 y="1" while x<len(str): print(str[x]+(y)) if str[x]==" ": if y=="1": y="2" elif y=="2": y="3" #etc x=x+1

4th Jan 2018, 6:59 AM
Steven
Steven - avatar
9 Answers
0
I got it fixed s = input() print(len(s)) x=0 y=1 while x<len(s): print(s[x]+str(y)) if s[x]==" ": y+=1 x=x+1 End program Thanks for your help
4th Jan 2018, 9:48 PM
Steven
Steven - avatar
+ 10
@Baptiste... right... I also said it.
5th Jan 2018, 12:15 PM
KäzÎ MrÎdÚl HøssäÎn
KäzÎ MrÎdÚl HøssäÎn - avatar
+ 9
str is a python function.. you define str as a variable. again you call str() function. when you call str like function, you will face a error. that time ask yourself... a variable is callable? you should know rules of defining a variable.
4th Jan 2018, 4:04 PM
KäzÎ MrÎdÚl HøssäÎn
KäzÎ MrÎdÚl HøssäÎn - avatar
+ 4
s = input() print(len(s)) x=0 y=1 while x<len(s): print(s[x]+str(y)) y = y + 1 x=x+1 I had to rename your string as str is a function to cast an object to string. You override it in your code but needed it
4th Jan 2018, 7:49 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
@KäzÎ, it is legal to do so in python and as he did not call it as a function, no problem
4th Jan 2018, 7:04 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Good idea @~ swim ~
4th Jan 2018, 11:00 PM
Steven
Steven - avatar
- 1
s = input() print(len(s)) x=0 y=1 while x<len(s): print(s[x]+str(y)) y+=(s[x]==" ") x=x+1 y=str(y) Even better without while s=input() y=str(sum(c==" " for c in s)) or even y=str(input().count(" ")) Str is a python keyword. Confusing to name a var after it.
4th Jan 2018, 9:33 PM
VcC
VcC - avatar
- 1
still awfully long. use what python offers !
4th Jan 2018, 10:09 PM
VcC
VcC - avatar