python programming help?

2009-02-09 10:59 pm
1)when using len(), how can i not count the space but only the words?
2)how can i Writes this sentence (i am jennifer) on standard output with all vowels in upper case and all consonants in lower case ?

回答 (2)

2009-02-10 11:22 am
✔ 最佳答案
Here you go:

s = "i am jennifer"

1)
>>> len(s.split()) # count words
3
>>> len([c for c in s if c != ' ']) # count non space chars
11

2)
>>> s.lower().replace('a', 'A').replace('e', 'E').replace('o', 'O').replace('i', 'I').replace('u', 'U')
'I Am jEnnIfEr'
2009-02-10 7:08 am
just do an "if" command (this is not python but it will give you the idea) if, "aeiou", print as "AEIOU". if "bcdfghjklmnpqrstvwxz", print
Just do that with a basic printing and input command.

heres a simple example game that I made

#!/usr/bin/env python

import random
import urllib

print 'time to play hangman'
print 'Category is, Animals'
animals = urllib.urlopen('http://davidbau.com/data/animals').read().split()
secret = random.choice(animals)
guesses = 'aeiou'
turns = 5

while turns > 0:
missed = 0
for letter in secret:
if letter in guesses:
print letter,
else:
print '_',
missed += 1

print

if missed == 0:
print 'You win!'
break

guess = raw_input('guess a letter: ')
guesses += guess

if guess not in secret:
turns -= 1
print 'Nope.'
print turns, 'more turns'
if turns < 5: print ' O '
if turns < 4: print ' \_|_/ '
if turns < 3: print ' | '
if turns < 2: print ' / \ '
if turns < 1: print ' d b '
if turns == 0:
print 'The answer is', secret
print 'Your dead, loser!'


收錄日期: 2021-05-01 01:10:21
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090209145949AA07oJ8

檢視 Wayback Machine 備份