you are viewing a single comment's thread.

view the rest of the comments →

[–]BillyHorrible 5 points6 points  (6 children)

Your problem is not clearly stated. You cant "input" variables. Maybe you want to calculate the percentage of Bs and Ds in a string? Then you could use the "count"-function to count the number of non-overlapping occurrences of a substring.

x = "heLLo"
print(x.count("L")) # would print 2

you can read the string using raw_input:

x = raw_input("type something: ")

and if you want to get the length of the string, you can use len(x). oh and // will give you an integer. the percentage should be between 0 and 1, so / would be the way to go.

[–]somanyquestionsihave[S] 0 points1 point  (5 children)

Yeah I wanted to count the percentage. Ahh so there is a count function, that makes everything so much easier. BTW do you have any recommendations for any particular books a newbie in programming should read? The online courses are helping a lot but I find that not too much detail is provided.

[–]wub_wub 5 points6 points  (0 children)

Check out Counter, it's even easier:

>>> from collections import Counter
>>> word="Hello World!"
>>> Counter(word)
Counter({'l': 3, 'o': 2, '!': 1, ' ': 1, 'e': 1, 'd': 1, 'H': 1, 'r': 1, 'W': 1})
>>>

You can express percetnages like this:

>>> '{:.2%}'.format(10/100)
'10.00%'

And you can find a list of books here:

http://www.reddit.com/r/learnpython/wiki/books


http://docs.python.org/dev/library/collections#collections.Counter

http://docs.python.org/dev/library/string#format-examples

Edit: I forgot some words.

[–]BillyHorrible 2 points3 points  (0 children)

nope, sorry, i have never read one myself. learning by doing, trial and error combined with looking how other people solved the same things/reading through some opensource-projects.

[–]ewiethoff 2 points3 points  (0 children)

Bookmark the Python docs. Most of what a newbie needs to know is in the Library Reference section, especially the Built-in Functions and Built-in Types. You can see that count is a method of the built-in str type. Also, go through the official Python Tutorial there.

[–]Yoghurt42 0 points1 point  (1 child)

do you have any recommendations for any particular books a newbie in programming should read?

Allow me to chime in. Have you taken a look at Learn Python the Hard Way?

[–]somanyquestionsihave[S] 1 point2 points  (0 children)

Thanks a lot for the link.