#๐Ÿ”’ 4. Read (Input) 10 names, find the occurrence of name (sam)

47 messages ยท Page 1 of 1 (latest)

tiny dome
#

how do i solve this???

mild boneBOT
#

@tiny dome

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quasi temple
#

What code do you have so far? We're not really in the business of dishing out solutions

tiny dome
#

relax buddy

#

bruh i have my code here it is

#

x=input("please enter 10 names ")
c=0
for x in range(10):
if x =="Sam":
c+=1
print("count of sam is :", c)

mild boneBOT
#

Hey @tiny dome!

It looks like you're trying to paste code into this channel.

Discord has support for Markdown, which allows you to post code with full syntax highlighting. Please use these whenever you paste code, as this helps improve the legibility and makes it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
amber jay
tiny dome
amber jay
#

Then I'll ask you, the designer of this program, how you would want it done?

tiny dome
#

oh one line

amber jay
#

Can you show an example?

tiny dome
#

do u mean this?

amber jay
#

yes

#

ok so you want a space in between each name

#

have you learned about list in python?

tiny dome
#

no only break and continue loops

#

till now

#

which i dont think i need them for this code right

amber jay
#

that's unrelated

#

yeah

tiny dome
#

yeah thats what i took till now

amber jay
#

have you learned str and int?

tiny dome
#

yes

amber jay
#

ok those list is another "data type". It can hold multiple elements

tiny dome
#

ohhhh okay

amber jay
#

we can "split" a string into a list

tiny dome
#

hm

#

so how do i use it?

amber jay
#
names = "sam sam sam sara sara sara ben ben ben bob"
#

so if these are our names (yours come from input but it's the same concept)

tiny dome
#

yes

amber jay
#

!e

names = "sam sam sam sara sara sara ben ben ben bob"
print(names.split())
mild boneBOT
#

@amber jay :white_check_mark: Your 3.12 eval job has completed with return code 0.

['sam', 'sam', 'sam', 'sara', 'sara', 'sara', 'ben', 'ben', 'ben', 'bob']
amber jay
#

this create a list of 10 elements

#

where each element in the list is one name

#

now we can use a for loop to access each name within the list and check them one by one

#

the for loop syntax is "for <variable> in <iterable>"

#

<variable> will be a new name that you assign to the elements of the list

#

so your original example had an issue there

#

you reused x for your input and your loop variable, which doesn't quite make sense

tiny dome
#

oh

amber jay
#

the for loop variable will always be a new name for the loop. You wouldn't place an existing variable's name there

amber jay
#

see if you can write the for loop knowing what you know now

mild boneBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.