Python: Beginning problems
ok so basically i very new to programming and have no idea how to go about these problems help if you will ^^
Numerologists claim to be able to determine a person’s character traits based on the “numeric value” of a name. The value of a name is determined by summing up the values of the letters of the name, where ‘a’ is 1, ‘b’ is 2, ‘c’ is 3 etc., up to ‘z’ being 26. For example, the name “Zelle” would have the value 26 + 5 + 12 + 12 + 5 = 60 (which happens to be a very suspicious number, by the way). Write a program that calculates the numeric value of a single name provided as input.
Word count. A common utility on Unix/Linux systems is a small program called “wc”. This program counts the number of lines, words (strings of characters separated by blanks, tabs, or new lines), and characters in a file. Write your own version of this program. The program should accept a file name as input and then print three nu开发者_StackOverflowmbers showing the count of lines, words, and characters in the file.
I won't give you an answer, I want to help you:
Read the tutorial!
What you ask for is pretty basic and should be perfectly covered by the tutorial, especially read about strings and reading files.
Disclaimer: @Charles Beattie I am ok with a downvote ;) If anyone considers this not to be a valid answer I'll put it as comment, just say so.
I'm not going to give you a full answer as well :)
I like Google's Python tutorial. In addition to small easy written tutorials, it has video lectures (which also covers strings and reading files). At the end of each topic, you can do simple exercises (provided on the tutorial page).
Questions like these should not be asked if they come from a book! usually the auther gives plenty of tips! However, after a year i think it's ok for you too see an approach that some would use.
Question 1
def answer1(name, value=0):
for char in name:
value += int(char, 36) - 9
print value
answer1(raw_input("give your name: ").lower())
Question 2
# place your python script in the textfile location or rewrite the path
file_all = open('C:\\somefile.txt')
value_lines = 0
value_words = 0
value_chars = 0
for lines in file_all:
value_lines += 1
for words in lines.split():
value_words += 1
for chars in words:
value_chars += 1
print value_lines, '\n', value_words, '\n', value_chars
this is the general idea you would want to use, good luck with it!
def WordCalc():
name = raw_input("Enter value: ")
value = 0
for char in name:
value += int(char, 36) - 9
print value
print name
WordCalc()
def main():
name = raw_input("Enter name: ")
value = 0
name = name.replace(" ", "")
for char in name:
value += int(char, 36) - 9
print value
print name
main()
If you have problem about space inside string you can use this code
精彩评论