2016-05-16 167 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不使用打印(引用),因爲它增加了空格

相關問題