(python) How to convert number into symbol ' * ' - python

I just thinking how to convert the numbers into the ' * ' (eg. if I enter 4 then **** and the result will be like this: ****, ***, **, *)
I know that the code should be like this:
number = int(input())
while number >= 0:
print (number)
number = number - 1
but how to make it become ' * '?
Thanks.

Try this:
print(number * '*')
It will print * number times. Example:
>>> print(4 * '*')
'****'

Another approach:
''.join('*' for _ in range(4))
However, as #GingerPlusPlus points out, this approach is slower than the other one overloading the * operator.

If I understand you correctly, you want to print the astericks * symbol X amount times based on the number entered and then you want to count down to 1? That explanation might be rough, so here's an example of what I believe you are asking for:
If the user enters 3, you want to print:
***
**
*
If that is correct then here's a possible implementation (coded for Python 3.x):
number = int(input())
while (number > 0):
print( '*' * number)
number -= 1
# End While

Related

Draw a centered triforce surrounded by hyphens using Python

I want to draw a triangle of asterisks from a given n which is an odd number and at least equal to 3. So far I did the following:
def main():
num = 5
for i in range(num):
if i == 0:
print('-' * num + '*' * (i + 1) + '-' * num)
elif i % 2 == 0:
print('-' * (num-i+1) + '*' * (i + 1) + '-' * (num-i+1))
else:
continue
if __name__ == "__main__":
main()
And got this as the result:
-----*-----
----***----
--*****--
But how do I edit the code so the number of hyphens corresponds to the desirable result:
-----*-----
----***----
---*****---
--*-----*--
-***---***-
*****-*****
There's probably a better way but this seems to work:
def triangle(n):
assert n % 2 != 0 # make sure n is an odd number
hyphens = n
output = []
for stars in range(1, n+1, 2):
h = '-'*hyphens
s = '*'*stars
output.append(h + s + h)
hyphens -= 1
pad = n // 2
mid = n
for stars in range(1, n+1, 2):
fix = '-'*pad
mh = '-'*mid
s = '*'*stars
output.append(fix + s + mh + s + fix)
pad -= 1
mid -= 2
print(*output, sep='\n')
triangle(5)
Output:
-----*-----
----***----
---*****---
--*-----*--
-***---***-
*****-*****
Think about what it is you're iterating over and what you're doing with your loop. Currently you're iterating up to the maximum number of hyphens you want, and you seem to be treating this as the number of asterisks to print, but if you look at the edge of your triforce, the number of hyphens is decreasing by 1 each line, from 5 to 0. To me, this would imply you need to print num-i hyphens each iteration, iterating over line number rather than the max number of hyphens/asterisks (these are close in value, but the distinction is important).
I'd recommend trying to make one large solid triangle first, i.e.
-----*-----
----***----
---*****---
--*******--
-*********-
***********
since this is a simpler problem to solve and is just one modification away from what you're trying to do (this is where the distinction between number of asterisks and line number will be important, as your pattern changes dependent on what line you're on).
I'll help get you started; for any odd n, the number of lines you need to print is going to be (n+1). If you modify your range to be over this value, you should be able to figure out how many hyphens and asterisks to print on each line to make a large triangle, and then you can just modify it to cut out the centre.

increase the amount of symbols by increasing an integer (python)

I am trying to display an indicator for the time taken and i want to show a progressbar.
Heres my current code:
time = time * 60
time = time / 100
x = 0
while x < 101:
per = chr(ord('█') + int(x))
per_ = chr(ord(' ') + int(100 - x))
await asyncio.sleep(time)
pr = (f'\rTime used: |{str(per)}{str(per_)}| {x}%')
print(pr, end="\r")
x = x + 1
But all i got is weird combination of symbols. I don't really know how to use chr or ord properly
> Time used: |▐ ↑| 8%
chr and ord convert between characters and integers representing that specific character. You don't need to use them here.
Also, * is used to repeat a string (and not +).
Something like this should work:
per = '█' * int(x)
per_ = ' ' * int(100 - x))

need help in understanding a code

Can anyone explain this code a little. I can't understand what n does here? We already have taken N = int(input()) as input then why n=len(bin(N))-2? I couldn't figure it out.
N = int(input())
n = len(bin(N))-2
for i in range(1,N+1):
print(str(i).rjust(n) + " " + format(i,'o').rjust(n) + " " + format(i,'X').rjust(n) + " " + format(i,'b').rjust(n))
n counts the number of bits in the number N. bin() produces the binary representation (zeros and ones), as as string with the 0b prefix:
>>> bin(42)
'0b101010'
so len(bin(n)) takes the length of that output string, minus 2 to account for the prefix.
See the bin() documentation:
Convert an integer number to a binary string prefixed with “0b”.
The length is used to set the width of the columns (via str.rjust(), which adds spaces to the front of a string to create an output n characters wide). Knowing how many characters the widest binary representation needs is helpful here.
However, the same information can be gotten directly from the number, with the int.bitlength() method:
>>> N = 42
>>> N.bit_length()
6
>>> len(bin(N)) - 2
6
The other columns are also oversized for the numbers. You could instead calculate max widths for each column, and use str.format() or an f-string to do the formatting:
from math import log10
N = int(input())
decwidth = int(log10(N) + 1)
binwidth = N.bit_length()
hexwidth = (binwidth - 1) // 4 + 1
octwidth = (binwidth - 1) // 3 + 1
for i in range(1, N + 1):
print(f'{i:>{decwidth}d} {i:>{octwidth}o} {i:>{hexwidth}X} {i:>{binwidth}b}')

python task: price in dollars and cents

As input, I have a number, price in dollars and cents (for example: 10.35) I need to print dollar and cents (based on above exmaple: 10 35). (input 10.09 - output 10 09
I created the code:
from math import floor, trunc
a = float(input())
r = trunc(a)
k = trunc(a * 100)
k = (k % 100)
if k <= 9:
print(r, "%02d" % (k,))
else:
print(r, k)
However, when I test it on automatic tester, one of the conditions is not working. I am not able to see input in the test, please could you tell me where do I have mistake?
You should test function not script. Problem probably caused by no correct test.
Try this:
from math import trunc
def to_new_format(price):
dolar_part = trunc(price)
cent_part = round(((price % 1) * 100), 3)
return "%i %i" % (dolar_part, cent_part)
input_price = float(input())
print(to_new_format(input_price))
If the input is a string 10.35 (as it seems to me) than you could also try the following (I know you were not asking this, but it might help nevertheless):
a = string(input())
b = a.split(".")
res = b[0] + " " + b[1]
print(res)
which outputs:
10 35

Creating pyramid using symbols in Python

This is the assignment:
Write a python function that accepts a character and an integer and then uses that character to create a triangular structure like the example below. Make sure that the number of lines is in the range 1 to 10 and that only the first character in the user entered symbol is used if they enter more than one character.
Symbol? *
Lines? 4
*
* *
* * *
* * * *
I've got all of it except the spacing right... here's what I figured out so far.
def Triangle():
lines = -1
while lines not in range(1,11):
symbol=input("Symbol? ")
lines=input("Lines? ")
for i in range(lines + 1):
spaces = lines - i
print ((' ' * spaces) + (symbol * i))
This prints out:
*
**
***
****
Can't seem to get this right... thoughts?
Also if anyone has ideas on how to ensure only the first character is used as the symbol as noted in the question, that'd be awesome.
You need add in spaces after each symbol:
print ((' ' * spaces) + ((symbol + ' ') * i))

Categories

Resources