How do i get these output?
def pair(str):
count = 0
for ch in str:
if ch == 'HLL':
return "Alice"
if ch == 'EO':
return "Bob"
if ch == "WORL":
return "Alice"
if ch == "D":
return "Bob"
else:
return "Hello World"
print(count)
CodePudding user response:
return
is executed before print
, which means this method ends before print
. print
is not reachable in your code.
CodePudding user response:
I don't know that you want to get data type of these output. So, I guess 2 data types and write them here.
- String
def pair(str):
count = 0
result = ''
for ch in str:
count = 1
if ch == 'HLL':
result = "Alice"
if ch == 'EO':
result = "Bob"
if ch == "WORL":
result = "Alice"
if ch == "D":
result = "Bob"
else:
result = "Hello World"
print(count)
return result
- List
def pair(str):
count = 0
result = list()
for ch in str:
count = 1
if ch == 'HLL':
result.append("Alice")
if ch == 'EO':
result.append("Bob")
if ch == "WORL":
result.append("Alice")
if ch == "D":
result.append("Bob")
else:
result.append("Hello World")
print(count)
return result
Since return is the code that ends the function(def), for loop is executed only once in your code
- For reference, if the input is a string(str), only one letter is entered in 'ch' in the for loop, so nothing other than D will be identified.