0

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

1
  • Doing for x in exampleA and exampleB: shows a fundamental misunderstanding of and which is a logical operator and so it amounts to asking if both the expressions "hello" and "50, 70, 80" are true. In natural language that makes no sense whatever, but Python has rules for converting such things to True and False, and like most implicit type coercions, it holds surprises for novices. If you want to know more, google for "Python truthy", but for now a simpler rule is don't use logical operators on strings (until you know what you are doing). Commented Jul 19, 2021 at 6:42

4 Answers 4

1

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) #
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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'

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.