Reputation: 1
My Code:
exampleA= input("Give me an ExampleA\n") #hello
exampleB = input("Give me an ExampleAB\n") #<- here you should be able to write 50, 70, 80
for x in exampleA and exampleB:
print("nice" + exampleA) #<- i get nicehello
print("for" + exampleB) #
so i want for each element in exampleB Another print, but I get this output
nicehello
for50,70,80,90,20
nicehello
for50,70,80,90,20
nicehello
for50,70,80,90,20
but I want
nicehello
for50
nicehello
for70
nicehello
for80
by the way I'm a beginner in python
Upvotes: 0
Views: 355
Reputation: 3011
Try this -
exampleA= input("Give me an ExampleA\n") #hello
exampleB = input("Give me an ExampleAB\n") #<- here you should be able to write "50, 70, 80")
for x in exampleB.split(','):
print('nice',exampleA)
print('for',x)
Result:
Give me an ExampleA
abcd
Give me an ExampleAB
1,2,3,4
nice abcd
for 1
nice abcd
for 2
nice abcd
for 3
nice abcd
for 4
Upvotes: 0
Reputation: 31
As you given :
for x in exampleA and exampleB:
print("nice" , exampleA) #it always print nicehello
print("for" , exampleB) #since exampleB is 2030405060 it just prints whole data
It doesn't splits it for splitting you should use variable 'x'
Upvotes: 0
Reputation: 2084
Change code to in this way:
exampleA= input("Give me an ExampleA\n") #hello
exampleB = input("Give me an ExampleAB\n").split(',') #<- here you should be able to write 50, 70, 80
for x in exampleB:
print("nice" + exampleA) #<- i get nicehello
print("for" + x) #
Upvotes: 1
Reputation:
You actually need to split the string and then iterate over the elements of the string:
exampleA= input("Give me an ExampleA\n") #hello
exampleB = input("Give me an ExampleAB\n").split(',') #<- here you should be able to write "50, 70, 80")
for x in exampleB:
print("nice" + exampleA) #<- i get nicehello
print("for" + x) #<- so i want for each element in exampleB
Output:
Give me an ExampleA
Hello
Give me an ExampleAB
12,23,34
niceHello
for12
niceHello
for23
niceHello
for34
Upvotes: 0