Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to delete character by character from a string (each time a single character to see what the string is going to look like)
var = 'string'
var1 = ''
cor = []
for i in range(0, len(var)):
varl = var[:i] + var[(i+1):]
cor.append(varl)
print (cor)
This is what am getting
['t', 'sr', 'sti', 'strn', 'strig', 'tring', 'sring', 'sting', 'strng', 'strig', 'strin']
I don't know why am getting the first 5 elements in the list, they should not exist.
Does anyone know how to fix this, Thanks.
There isn't really any reason for this not to work. However, using list comprehension instead, seeing as it solved your problem:
var = 'string'
cor = [var[:i] + var[i+1:] for i in range(len(var))]
print (cor)
Returns
['tring', 'sring', 'sting', 'strng', 'strig', 'strin']
The main reason your output seems strange is the loop which should add len(var) variables max.
for i in range(0, len(var)):
In your variable definitions, you have the second variable defined as var1 (i.e. var'one'), and in your for loop, you have varl (i.e. var'el').
Change varls in your loop to var1 and you'll have what you expect.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 months ago.
Improve this question
I'm trying to write a program that has a function that takes in a string and prints the number of capital letters in the first line, then prints the sum of their indices in the second line.
Here is what I came up with but I am getting errors. Anybody knows what I can fix on this code to make it run?
import sys
def string(s):
a={"UPPER":0}
for b in s:
if b.isupper():
a["UPPER"]+=1
print ("No. of Upper case characters : ", a["UPPER"])
ind = [idx for idx in range(len(val)) if val[idx].isupper()]
Sum=sum(ind)
print(Sum)
val = input("")
string(sys.argv[1])
The issue with your code is val is declared outside the scope of function.
You can rewrite your function like this.
def string(s):
ind = [idx for idx, i in enumerate(s) if i.isupper()]
print(f"Sum of index of upper case character : {sum(ind)}")
print(f"No. of Upper case characters : {len(ind)}")
Execution:
In [1]: string('Hello World')
Sum of index of upper case character : 6
No. of Upper case characters : 2
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I have defined a fairly simple function that is having an unexpected output. I'm using Steam game reviews and want to use a function to narrow the scope based on game title. This works fine:
one_game = games[games['title'] == "Robocraft"]
The output is a dataframe just like the original. However, when I try to make a function (by passing the same game name as an argument) to slice the dataframe as follows:
def slice(game):
out = games[games['title'] == game],
return(out)
I get a tuple that is "[362 rows x 5 columns],)" instead of a dataframe. Is that because of the return command or is that just something that happens when you use a user defined function?
This seems like all I would need to do is convert the tuple back to a dataframe. However, I can't even do that! When I do, I get this error:
"ValueError: Must pass 2-d input. shape=(1, 362, 5)"
How can I get a dataframe as my output?
Thank you!
The comma at the end of the first line of your function is the problem. It wraps the preceding value in a tuple, see:
>>> 1
1
>>> 1,
(1,)
>>> a = 1
>>> type(a)
1
>>>> a = 1,
>>> type(a)
tuple
So just remove that comma, (and the parentheses after return, because return is a keyword, not a function):
def slice(game):
out = games[games['title'] == game]
return out
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have wrote this Code for calculates the product of the first natural numbers, but it showing answer 1 every time. I don't where i did mistake?? Can you please help me find out my mistake in this code..
num = 10
i = 0
prod = 1
while i<=num:
i = i+1
prod*prod*i
print(prod)
The problem seems to be on the line prod*prod*i. The product needs to be accumulated and for this it should be exchanged for prod*=i.
The new snippet is:
num = 10
i = 0
prod = 1
while i<=num:
print(i)
i = i+1
prod*=i
print(prod)
Instead of prod*prod*i write prod=prod*i
Here we first take the input of the number of terms.Then we iterate the for loop and multiply the value of x with the initial value(=1).Then we assign the new value to p.
n=int(input('Terms: ')) #specifing the limit
p=1
for x in range(1,n+1):
p=p*x
print(p)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Check out the screenshot and help this newb with why i'm getting this syntax error with the for loop even though im following the right syntax.
The code :
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: ')
for i in range(0,n):
ele=int(input())
lst.append(ele)
The Error : Invalid Syntax for the ':' after 'range(0,n)'
You are spacing the items inside the for loop with double Tab, the indentation should be either 4 spaces or a single tab.
And you are missing a parenthesis closing in the n input line
See the modified code below.
elif choice == 'AVERAGE':
import statistics
lst = []
n = int(input('Enter number of values to calculate mean of: '))
for i in range(0,n):
ele=int(input())
lst.append(ele)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
This is the code I am trying to run :
line = "123456789"
p = 2
print line[p,p+2]
And I get the error - TypeError: string indices must be integers, not tuple. How can I use line[ , ] with variables. Any help is appreciated.
You want to use colons for slicing.
line = "123456789"
p = 2
print line[p:p+2]
That works fine.
Output:
34
line = "123456789"
p = 2
print line[p,p+2] # this is incorrect slice notation
the correct form is:
print line[p:p+2] # with a colon
look here for info on strings and string slicing