Program that not working properly [closed]
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
开发者_运维知识库 Improve this questionI need to write some program that it count until it's found space when its found space the program break to work.
I have been problem. my code:
x = raw_input("")
i = 0
corents = 0
while(x[i] != " "):
corennts +=i
i+=1
name = x[:corents]
print name
If I will input string "Hola amigas" its returns "Hola". I'm need the program without some built-ins/or imports file function.
I need to implement it only with while/for loops.
corents
is spelt wrong, 5th line down.
x = raw_input("")
i = 0
corents = 0
while(x[i] != " "):
corents +=i
i+=1
name = x[:corents]
print name
x = raw_input()
name = x.split(' ', 1)[0]
print name
or
x = raw_input()
try:
offs = x.index(' ')
name = x[:offs]
except ValueError:
name = x # no space found
print name
The pythonic way of doing this is like this:
x = raw_input("")
name = x.split(" ")[0]
print name
The split
method splits the string into an array, and [0] returns the first item of that array. If you for some reason need the index:
x = raw_input("")
i = x.index(" ")
name = x[:i]
print name
精彩评论