2016-05-16 111 views
0

我是一个初学者在python。我在codewars中遇到了这个问题。ValueError:无效文字int()与基地10

贾登以他通过Twitter提供的一些哲学而闻名。在Twitter上撰写文章时,他几乎总是把每一个词都用在大写字母上。 您的任务是将字符串转换为Jaden Smith的书写方式。这些字符串是来自Jaden Smith的实际报价,但它们没有按照他原先输入的方式进行大写。

例子:

不贾登外装:“镜子怎么能是真实的,如果我们的眼睛都不是真正的”

贾登外装:“镜子怎么可能是真的。如果我们的眼睛都不是真正的”

这是我尝试(我应该使用功能代码)

def toJadenCase(string): 
    l = len(string) 
    for i in range(0,l): 
     if string[i] == ' ': 
      y = string[i] 
      string[i+1] = chr(int(y)-32) 
    return srting 
s = raw_input() 
print toJadenCase(s) 

运行时,下面的错误出现了

How can mirrors be real if our eyes aren't real (this is the input string) 
Traceback (most recent call last): 
    File "jaden_smith.py", line 9, in <module> 
    print toJadenCase(s) 
    File "jaden_smith.py", line 6, in toJadenCase 
    string[i+1] = chr(int(y)-32) 
ValueError: invalid literal for int() with base 10: '' 

即使谷歌-ING它,我无法理解这些错误。任何帮助,将不胜感激。如果我的代码中的其他错误突出显示,并且建议了更好的代码,我也会很棒。

在此先感谢:d

+3

你为什么试图将空格转换为int?你的意思是'y = string [i + 1]'?而不是'int()'你的意思是'ord()'? – Natecat

+2

您试图将消息中的每个空格转换为int。 ''''的整数值是多少? –

+0

这是一个错误,我的意思是将字符串[i + 1]转换为int。 – Kaushik

回答

1

作为超值指出,串不应该被用来作为变量名

Zen of Python,这在技术上是,做你想要什么功能实现:

def toJadenCase(quote): 
    return quote.title() 

编辑:

修订版本锡永处理撇号:

import string 

def toJadenCase(quote): 
    return string.capwords(quote) 
+0

关闭,但它也大写撇号后的t – Vincenzooo

+0

这是行不通的。使用'string'模块中的'capwords()'。例如:'titleize = lamda s:__import __(“string”)。capwords(s)'。我宁愿导入它,也可能使用一个真正的函数,但我有点受限于atm。 – Goodies

+0

这也是为什么你不应该使用'string'作为参数名称。 – Goodies

0

首先,你必须明白,字符串是不变的,所以你不能设置一个字符串中的单个字符,而是建立从旧的新字符串和替换旧的(这通常可以一次完成,所以这不是一个很大的复杂因素)。其次,对于大多数这类操作,使用字符串对象本身的方法要好得多,而不是从头开始重做所有事情。

说,仍然有一些并发症有问题,而是你想要做什么的功能是模块字符串:

import string 
s="How can mirrors be real if our eyes aren't real" 
newstring=string.capwords(s) 

如果你喜欢(为什么?)一个DIY解决方案(使用字符串方法):

newstring=' '.join([ss.capitalize() for ss in s.split()]) 

注意,使用split没有参数分割上任何空白(字符串如标签等),我认为是所需的行为。

0

如果要做到这一点,而无需使用一个已经存在的功能,这是我会怎么做,我会解释一切:

假设你只有基于文本的单词串和所有单词开始一个字符*现在

def toJadenCase(string): 
    words = string.strip().split() 
# This first strips all empty spaces around the words in the text and then splits the string by spaces (default) otherwise you can add a character inside split in order to split it at the character. This returns a list of words in the sentence. 
    li = [] # initialize empty list 
    for word in words: 
     word = chr(ord(word[0])-32) + word[1:] 
# So there's a couple of things going on here. 
# I could use .upper() to upper case something (like word[0].upper() + word[1:] 
# in order to get it but I wanted to do it without the use of that. 
# That being said, ord just figures out the ascii number and subtracting 
# 32 makes it uppercase. chr changes it back to a string. 
# Then it can be concatenated to the rest of the word. 
# Strings can be treated as lists in python so word[0] and word[1:] works 
Also, word[1:] just means from the 1st index to the end. 
    li.append(word) # this appends the word to the list 
    return ' '.join(li) # this joins all of the words in the list with a space 

,如果你想要的东西很多更简洁(你可以使用.capitalize()):

def toJadenCaseShort(string): 
    return ' '.join([x.capitalize() for x in string.strip().split()]) 

返回:

>>> abc("hello my friends") 
'Hello My Friends' 

基本上它所做的是使用列表理解去除然后拆分单词,将它们大写,然后将它们与空格结合起来!

当然,你可以使用string.title()作为标记。说,但那有什么乐趣? :)

0

下面是我通过了答案

import string 
 
def toJadenCase(str): 
 
    quote = string.capwords(str) 
 
    return quote #Do not use print(quote) as it adds spaces

高清toJadenCase(STR): 报价= string.capwords(STR) 回报报价#Do不使用打印(引用),因为它增加了空格

相关问题