Strings

This is the place for queries that don't fit in any of the other categories.

Strings

Postby el_manu » Mon Aug 26, 2013 8:51 am

program to ask user for a string in capital letters, and print out something. A string is music if it contains only the letters A through G.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby el_manu » Mon Aug 26, 2013 8:52 am

should i split each letter of the input and check for letters that aren't in A - G
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby stranac » Mon Aug 26, 2013 9:40 am

Just loop through all the letters. If you run into any that are not A-G, it's not music.

And please try to write one longer post instead of many short ones.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Strings

Postby el_manu » Mon Aug 26, 2013 10:24 am

a for or a while loop??
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby stranac » Mon Aug 26, 2013 11:04 am

Just write a loop.
Try doing stuff.
Then, if you fail, ask for help.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm

Re: Strings

Postby el_manu » Mon Aug 26, 2013 12:17 pm

how would a loop help??
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby Mekire » Mon Aug 26, 2013 12:57 pm

Just write something showing you can figure out how to loop over the string. Post it. You'll get no more help until you do. Ever time you post back we want to see what you have tried. If you haven't tried anything new then we are wasting time trying to help you.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Strings

Postby el_manu » Mon Aug 26, 2013 1:09 pm

Mekire wrote:Just write something showing you can figure out how to loop over the string. Post it. You'll get no more help until you do. Every time you post back we want to see what you have tried. If you haven't tried anything new then we are wasting time trying to help you.

-Mek


I don't know what loop over string means!!!

this is some stuff i was given, looping over characters.

Time to combine some of our skills. We want to use our understanding of strings, the len function and while loops to print out each character of a string, one per line.
To do this, we can create an index variable, i and loop from 0 up to (but not including) the length of the string, which we can find using len(s):
Code: Select all
word = 'lock'
i = 0
while i < len(word):
  print(i, word[i])
  i = i + 1


but i don't even get what that means and i don't know how to loop over a string, and how to loop over a string to help me in my question..
i am really stuck.
i don't know much python:

all i know is: -

Print a string
Recognise a syntax error
Store things in variables
Read input from the user

Use mathematical operations (+ - * / **)
Use strings
Find the length of a string
Convert between numbers and strings
Print out numbers and strings

Use the if/elif/else control structure
Understand the difference between assignment and comparison
Make numerical comparisons using (> < = != >= <=)
Use nested if statements

Check for substrings within a string
Use strings in conditional expressions in if statements
Change the case of a string
Replacing parts of a string

Loop over the characters in a string
Loop over a range of numbers
Printing output from a loop on one line
Sum numbers using a loop

Understand the difference between floats and integers
Convert between floats and integers
Maths module
Use slicing to extract a substring from a string
String methods: startswith, endswith, strip

Looping while something is true: while loops
Using counters to step in loops
Infinite loops
Reading multiple lines from the user
Looping over characters
Factorisation: worked example

Access and modify elements in a list
Sort and reverse a list
Create a list to store multiple values
Loop while a condition is true
Use while loops to loop over a range of numbers
Identify an infinite loop
Use a while loop to read input from the user
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby el_manu » Mon Aug 26, 2013 1:16 pm

and i have been wasting my whole day doing these instead of studying for my yearlies :D :D :D :D :D :D :D :D :D :D :D :lol: :lol: :lol:
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby Mekire » Mon Aug 26, 2013 1:25 pm

When looping over any itterable (strings, lists, dictionaries, sets, etc.), for loops are preferred over while loops in python.

Loop directly over the elements:
Code: Select all
for i in "some string":
    print(i)

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Strings

Postby micseydel » Mon Aug 26, 2013 9:07 pm

In every language I've used that uses loops extensively (instead of recursion, such as Lisp or Prolog), for loops are preferable to while loops. Python just shows off how great ours are with itertools.

el_manu: are you doing this for a class, or for yourself? If it's for a class, you need to seek an in person tutor who has plenty of time to figure things out with you. If you're doing it for yourself, read tutorials, watch Youtube videos, or whatever helps you learn to form a basis, because that's not something we should be doing for you. (Tutorials and Youtube videos can also help even if you're taking a class, but if you're on a time schedule I do recommend one-on-one help on top of those.)
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1179
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Strings

Postby el_manu » Tue Aug 27, 2013 9:54 am

i am sure by now you guys are like....who is this el_manu guy. GIVE this guy a MANUAL. so i will make a last deal. if someone actually gives me the answer to this question, I will not bother the forums with any other questions or posts and you won't here of el_manu again until i actually READ A MANUAL.

please i really need it. i need it badly. who cares about the rest of the questions i NEED this one.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby Mekire » Tue Aug 27, 2013 10:04 am

I showed you how to loop through the string. Loop through it and use an if statement. Just try something..........

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Strings

Postby el_manu » Tue Aug 27, 2013 10:25 am

Code: Select all
a = input("Music? ")
b = a.upper()
if "H" or "I" or "J" or "K" or "L" or "M" or "O" or "P" or "Q" or "R" or "S" or "T" or "U" or "V" or "W" or "X" or "Y" or "Z" in b:
  print("That's not music!")
else:
  print("That's music!")

why won't this work
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby el_manu » Tue Aug 27, 2013 10:31 am

Code: Select all
a = input("Music? ")
if "H" "I" "J" "K" "L" "M" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" in a:
  print("That's not music!")
else:
  print("That's music!")


did it i think
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby Mekire » Tue Aug 27, 2013 10:32 am

It won't work because that is not how if statements work.

The statement:
Code: Select all
if "a" or "b" in some_string:
always runs because the conditional is always true.

The parts of the statement are separate.
The conditions are
Code: Select all
if "a":
and
Code: Select all
if "b" in some_string
The first statement is always True because it is a non-empty string.

Anyway, you are attempting to do the problem backwards. Loop through the string and check if each letter is in the accepted letters for music.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Strings

Postby el_manu » Tue Aug 27, 2013 10:33 am

stop locking posts. arrrggh i wanna edit the cuz they are wrong especially he one right above

Mekire: I will stop locking your posts when you realize that the edit button is for editing the post and not completely changing the content.
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby Mekire » Tue Aug 27, 2013 10:37 am

el_manu wrote:
Code: Select all
a = input("Music? ")
if "H" "I" "J" "K" "L" "M" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z" in a:
  print("That's not music!")
else:
  print("That's music!")


did it i think


No. Now you are checking if the string "'HIJKLMOPQRSTUVWXYZ'" is in the given string, which isn't what you want.

Use a loop. Use. A. Loop. USE A LOOP. UsE a LoOp.

-Mek
User avatar
Mekire
 
Posts: 986
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Strings

Postby el_manu » Tue Aug 27, 2013 10:40 am

i have all my notes and stuff from my tutor and stuff, but i don't really get the for loops. so looping through this and do that is like chinese. i have taken codeacademy.com and countless other websites as well as reading book but is STILL DON't GET what FOR LOOPS DO AND ARE AND HOW TO USE THEM FOR THIS QUESTION.............i am frustrated because you are trying to help and giving good advice are but I am that dumb that I am beyond help and it is making me retarted.

some programs i have made with for loops

Code: Select all
a = int(input("Time to launch: "))
print("Counting down ...")
for a in range(a, 0, -1):
   print(a, "...")
print("Blast Off!")

Code: Select all
a = int(input("Number of lines: "))
for b in range (1, a + 1):
    print(b * '*')


Code: Select all
a = int(input("a: "))
output = ""
for a in range(1, a+1,):
  output = output + str(a*3*(a-1)+1) + ' '
print(output[:-1])
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Re: Strings

Postby el_manu » Tue Aug 27, 2013 10:46 am

Code: Select all
a = input("Music? ")
b = a.upper()
if "H" in b or "I" in b or "J" in b or "K" in b or "L" in b or "M" in b or "N" in b or "O" in b or "P" in b or "Q" in b or "R" in b or "S" in b or "T" in b or "U" in b or "V" in b or "W" in b or "X" in b or "Y" in b or "Z" in b:
  print("That's not music!")
else:
  print("That's music!")


DONE
el_manu
 
Posts: 87
Joined: Mon Aug 19, 2013 8:30 am

Next

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests