Answer to Question #159663 in Python for Meera

Question #159663

(a) Write a program which will ask the user how many symbols to output, and then output that many lines of symbols (one symbol on each line). To start, use the '*' symbol. (1K)


(b) Write a program which will ask the user how many symbols to output, and then it will output that many symbols on one line. To start, use the '*' symbol. (1K)


For example, if the user enters 5, the output would be:

*****

(c) Output the user's number as well as the symbols, all on the same line. For example, if the user entered a value of 10, the output would be: (1K)


10 **********


(d) Modify your program to allow the user to choose a symbol as well. (1K)

Number of symbols? 7

Symbol to use? @

7 @@@@@@@


(e) Allow the user the specify the total number of symbols and how many to print on each line.

For example, if the user entered 10 symbols with 4 per line, the output might be: (2T)

$$$$

$$$$

$$

(the last line only has two symbols because we only print 10 total)

'''



symbol = '*'

numOfSymbols = int(input("how many symbols do you want: "))


#A)

for count in range(numOfSymbols):

print(symbol)



#B)

print(symbol*numOfSymbols)



#C)


1
Expert's answer
2021-01-30T22:36:05-0500

Hi!

Here is my answer.


def func_a():
   """
   Ask the user how many symbols ('*') to output
   Output that many lines of symbols (one symbol on each line)
   """
   symbol = '*'
   while True:
      try:
         numOfSymbols = int(input("How many symbols do you want: "))
         if numOfSymbols <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer') 

   for count in range(numOfSymbols):
      print(symbol)

def func_b():
   """
   Ask the user how many symbols ('*') to output
   Output that many symbols on one line
   """
   symbol = '*'
   while True:
      try:
         numOfSymbols = int(input("How many symbols do you want: "))
         if numOfSymbols <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer!') 

   print(symbol * numOfSymbols)

def func_c():
   """
   Ask the user how many symbols ('*') to output
   Output the user's number as well as the symbols, all on the same line
   """
   symbol = '*'
   while True:
      try:
         numOfSymbols = int(input("How many symbols do you want: "))
         if numOfSymbols <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer!') 

   print(numOfSymbols, symbol * numOfSymbols)   

def func_d():
   """
   Ask the user how many symbols to output
   Ask the user to choose a symbol
   Output the user's number as well as the symbols, all on the same line
   """
   while True:
      try:
         numOfSymbols = int(input("How many symbols do you want: "))
         if numOfSymbols <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer!')

   while True:
      try:
         symbol = str(input("What symbol do you want to use: "))
         if len(symbol) != 1:
            raise ValueError
         else:
            break
      except ValueError:
         print("Error: Please, only a string (one symbol in length)!")

   print(numOfSymbols, symbol * numOfSymbols)

def func_e():
   """
   Ask the user to specify the total number of symbols to output
   Ask the user how many symbols to print on each line
   Ask the user to choose a symbol
   Output that many symbols on each line in total
   """
   while True:
      try:
         numOfSymbols = int(input("How many symbols do you want in total: "))
         if numOfSymbols <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer!')

   while True:
      try:
         numOfSymbolsPerLine = int(input("How many symbols per line: "))
         if numOfSymbolsPerLine <= 0:
            raise ValueError
         break
      except ValueError:
         print('Error: Please, only a positive non-zero integer!')         

   while True:
      try:
         symbol = str(input("What symbol do you want to use: "))
         if len(symbol) != 1:
            raise ValueError
         else:
            break
      except ValueError:
         print("Error: Please, only a string (one symbol in length)!")

   '''
   # 1st method to output with iteration over an entire string of symbols
   symbols = symbol * numOfSymbols
   symbolsPerLine = ''

   for sym in symbols:
      symbolsPerLine += sym # accumulate symbols into a line
      if len(symbolsPerLine) == numOfSymbolsPerLine: 
         print(symbolsPerLine) # print a line if amount of symbols is enough
         symbolsPerLine = ''

   if symbolsPerLine: # print the last (shortened) line if exists
      print(symbolsPerLine)
   '''

   # 2nd method to output with slicing of entire string of symbols
   symbols = symbol * numOfSymbols
   start, end = 0, numOfSymbolsPerLine # indices' initialization
   while symbols: # while string is not empty do iteration
      print(symbols[start:end])
      symbols = symbols[end:]   # truncate string of symbols   

def main():
   func_a()
   print()
   func_b()
   print()
   func_c()
   print()
   func_d()
   print()
   func_e()

if __name__ == '__main__':
   main()      

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS