This question already has answers here:
Replace console output in Python
(12 answers)
Closed 11 months ago.
If I for example had a code like this:
score = 0
loop = true
while loop:
score = (score) + 1
print(score)
But I wanted to only print the new value of score instead of a long list of previous values aswell how would I do that?
print("Something", end='\r')
This way, any successive output will overwrite this one.
score = 0
while True:
score += 1
print(f"{score} ", end='\r')
Notice that in Python true is undefined, and unlike C, C++, JavaScript and some other languages, True and False are uppercase.
Related
This question already has answers here:
Short description of the scoping rules?
(9 answers)
Closed 1 year ago.
def happy(num):
while len(str(num))>1:
finding=hfind(num)
if finding==1:
print("True")
else:
print("false")
def hfind(num):
total=0
for i in str(num):
total+=int(i)*2
num=total
return(num)
happy(100)
I wrote code for Happy Number but,i don't know why it is not printing any output.
Can anybody explain clearly especially using this problem.
You need to reassign the value of num inside the loop.
Use ** 2 to square a number.
You need to check if the number reached 4 to prevent an infinite loop.
def happy(num):
while num != 1 and num != 4:
num = hfind(num)
if num == 1:
print("True")
else:
print("false")
def hfind(num):
total=0
for i in str(num):
total += int(i) ** 2
return total
happy(100)
This question already has answers here:
Putting an if-elif-else statement on one line?
(14 answers)
Closed 2 years ago.
I have the following Code in Python:
def show_sequence(n):
if n > 0:
return "+".join((str(i) for i in range(n+1))) + " = %d" %(sum(range(n+1)))
elif n == 0:
return "0=0"
else:
return str(n) + "<0"
Question: Is there a syntax correct way of putting all lines into one return statement if there are 3 if-statements?
I know it works with one if- & else-statement but im a fan of one-liner and already asked this myself several times.
Inline if-statements can be chained like this:
"a" if 0 else "b" if 0 else "c"
(replace the 0s with 1s to see the return value change)
You can use this:
result = True if a==b else False
This question already has answers here:
Reading in integer from stdin in Python
(4 answers)
Closed 4 years ago.
I am new to python and I am trying run this piece of code, however, the while loop doesn't seem to be working. Any ideas?
def whilelooper(loop):
i = 0
numbers = []
while i < loop:
print "At the top i is %d" %i
numbers.append(i)
i += 1
print "numbers now:",numbers
print "At the bottom i is %d" %i
print "the numbers:",
for num in numbers:
print num
print "Enter a number for loop"
b = raw_input(">")
whilelooper(b)
Your input is inputted as a string type, but the comparator
while i < loop:
is expecting both i and loop to be of type int (for integer), in order to be able to compare them.
You can fix this by casting loop to an int:
def whilelooper(loop):
i = 0
numbers = []
loop = int(loop)
...
This question already has answers here:
Python: Problem with raw_input reading a number [duplicate]
(6 answers)
Closed 5 years ago.
I am trying to print "count" , when count is smaller than my input value , but when I give the input value for X, it loos forever.
Can anybody tell me why ?
count = 0
x= raw_input()
while count <x :
print (count )
count +=1
By looking at the behaviour of the comparison operators (<, >, ==, !=), you can check that they treat integers as being smaller than non-empty strings. raw_input() returns a string (rather than an integer as you expected), hence your while loops indefinitely. Just switch to input():
count = 0
x = input()
while count < x:
print(count)
count += 1
Alternatively, you can use int(raw_input()), but I always use (and prefer) the former. All this is assuming you use Python 2.
Cast the input as an int so that the loop can increment it:
count = 0
x = int(raw_input())
while count <x :
print (count )
count +=1
This question already has answers here:
python: restarting a loop
(5 answers)
Closed 7 years ago.
I would like to know how do I start a for-loop from 0 if the if condition is True
for i in range (3):
if a=1:
#leave if-condition and start from beginning in for-loop with i=0
Break doesn't help here, because with break I only can leave the if Condition, but I also want to start the for loop from beginning.
You can try this instead of the for loop.
i = 0
while i < 3:
if a == 1:
i = 0
i += 1
This keeps walking through the loop if a equals 1
I think you may write like this
if 1 == a:
for i in range(3):
pass