#๐ 4. Read (Input) 10 names, find the occurrence of name (sam)
47 messages ยท Page 1 of 1 (latest)
@tiny dome
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.
What code do you have so far? We're not really in the business of dishing out solutions
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)
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.
There's a few issues here. Do you want the user to input all 10 names on one line, or do you want 10 separate inputs?
the question didnt specify so idk
Then I'll ask you, the designer of this program, how you would want it done?
oh one line
Can you show an example?
do u mean this?
yes
ok so you want a space in between each name
have you learned about list in python?
no only break and continue loops
till now
which i dont think i need them for this code right
yeah thats what i took till now
have you learned str and int?
yes
ok those list is another "data type". It can hold multiple elements
ohhhh okay
we can "split" a string into a list
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)
yes
!e
names = "sam sam sam sara sara sara ben ben ben bob"
print(names.split())
@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']
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
oh
the for loop variable will always be a new name for the loop. You wouldn't place an existing variable's name there
ohhh okay thanks
see if you can write the for loop knowing what you know now
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.