Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
a = 3
for i in range(a):
print(a, end = ' ')
a +=1
It just produces the output as:
3 4 5
I don't understand this because since a is being incremented each time, the loop should run forever.Or is it that the iterable range is generated only once?
You are confusing c/c++/c# for syntax with python.
In c/c++/c# you have a conditional inside the for syntax:
for (var i= 0; i<100;i++) # this is checked each time you come back here, incrementing
# the i will skip some runs and change how often the for is done
Pythons for is more a foreach:
for i in range(3):
==>
foreach(var k in new []{0,1,2}) # it takes a fresh "k" out every time it comes here
{ ... }
if you modify k it will still only run 3 times.
It has to do with how the program source code is interpreted. range(a) will be executed before the body of the loop, producing an iterable object which yields 3, 4, 5. a will be modified later but it will not affect range(a) cause it has already been executed. The following will do what you want, but it's kind of a silly program now:
a = 3
i = a
while i < a:
print(a, end = ' ')
a += 1
i += 1
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In this code first it take d=0 and l = 0 and then it write d=d+1 and l=l+1 do in this code what its uses in this code ? ]1
In this code first it take d=0 and l = 0 and then it write d=d+1 and l=l+1 do in this code what its uses in this code ?
Go through it line by line - first you run a loop where each character of the input is sequentially represented as i.
Now, for each i, if i is a digit, you increase the count of d - using d=d+1.Otherwise (elif) if i is an alphabet, you increase the count of l - using l=l+1.
This way you're storing the number of digits in the input as d, and the number of letters in the input as l.
Finally, after the loop runs on all character of the input, you print the number of letters and digits respectively using print("letter",l,"digit",d)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Im new to python.i have absolutly no idea how to solve this:
Extends the list with a While loop or For loop, so that the next entry is the sum of the last entry and the previous entry:
myListe = [1,2,3]
Deducing from your given values, you are starting out from
myListe = [1, 2, 3]
# ^ ^
# index -3 -1
and are supposed to add the last element and the one two before that.
This, you can achieve via:
while True:
next_value = myListe[-3] + myListe[-1]
if next_value > 1000:
break
myListe.append(next_value)
You can verify that the last value in the list is then in deed 872.
myListe[-3:]
# [406, 595, 872]
Using some knowledge about built-in utils, slices and named assignments (Python >= 3.8), you can simplify the while loop:
while (next_value := sum(myListe[-3::2])) <= 1000:
myListe.append(next_value)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
New to python and trying to understand what does following syntax doing ?
def testMissingConfig(self):
""" if config is missing, the config is valid """
input_args = self.buildmock()
validation_errors = [
x
for x in self.validator.validate(
ValidatorArguments(input_args=input_args)
)
if x
]
validation_keys = {x.key for x in validation_errors}
self.assertEmpty(validation_keys)
Especially the array initialization for "validation_errors"
It is called List comprehension. Here you can combine assignments, loops, functions all in one block.
One of the big advantages of list comprehension is that they allow developers to write less
code that is often easier to understand.
Syntax:
[expression for item in list]
Example:
number_list = [ x for x in range(20) if x % 2 == 0]
print(number_list)
Here the numberlist loops over 0 to 20 and gives even numbers 0,2,4...20 as result.
Similarly in your code, validation_errors will store x if x exists(not null)
Lambda functions can also be used to create and modify lists in less lines of code.
Reference:
https://www.programiz.com/python-programming/list-comprehension
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
this code is meant to 'explode' a given string s
def string_splosions(s):
"""erp9yoiruyfoduifgyoweruyfgouiweryg"""
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
For some reason this code can return the correct 'explosion' for most words however words which have a repeated letter they do not print correctly.
examples.
Correct outputs is would be:
Code --> CCoCodCode
abc --> aababc
pie --> ppipie
incorrect outputs when s is
Hello --> HHeHelHelHello (should be HHeHelHellHello)
(Note: in the incorrect output there should be 1 more l in the second to last repeat.)
You should transcribe the code instead of posting a picture:
def string_splosion(s):
new = ''
for i in s:
new += s[0:int(s.index(i))+1]
return new
The problem is that index(i) returns the index of the first instance of that character, which is 2 for both l's in "Hello". The fix is to just use the index directly, which is also simpler:
def string_splosion(s):
new = ''
for i in range(len(s)):
new += s[:i+1]
return new
Or even:
def string_splosion(s):
return ''.join(s[:i+1] for i in range(len(s)))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Write a while loop that sums the values 1 through end, inclusive. end is a variable that we define for you. So, for example, if we define end to be 6, your code should print out the result:
21
which is 1 + 2 + 3 + 4 + 5 + 6.
Is anybody able to guide me through this, without spoiling it for me?
There are two things you can do. The "fast" way (a la the story about the young Gauss) recognizes that
sum(1:N) = N * (N + 1) / 2
But I doubt that is what is asked.
You need to create a loop (look at the for command) over a range (look at the range command), and in each iteration add the current value of the loop variable to the sum (which you initialize to zero before the start of the loop).
There - you should now be OK.
EDIT with a while loop, and still leaving you to do a little bit of work:
mySum = 0
i = 1;
while( <<< put some condition here >>> ):
mySum = mySum + i
<<<<< do something clever with i >>>>>
print <<<<< what do you think you should print here? >>>>>
Note that indentation is important in Python, and the : at the end of the while statement matters