PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 16, 2022

[FIXED] How to make the output appear vertically?

 August 16, 2022     output, python     No comments   

Issue

import random
Bins=1
while Bins!=0:
  Bins = input("Masukkan jumlah bin antara 1-25(0 untuk berhenti): ")
  Bins = eval(Bins)
  if Bins==0:
    print("Done! Program selesai.")
    break
  elif Bins>1:
    x=[]
    c=random.randint(0,10000)
    for i in range(Bins):
      x.append(c)
      c=random.randint(0,10000-c)
    if sum(x)!=10000:x.append(10000-sum(x))
    else:x.append(0)
  elif Bins==1: x=[10000]
  if Bins<0 or Bins>25:
    print("Maaf, hanya menerima angka 1-25")
    continue
  tmp=-1
  for i in range(Bins):
    tmp=round(100/Bins*i)+1
    tmp1=round(100/Bins*(i+1))
    print((str(tmp)+"-"+str(tmp1)).rjust(6)+" :"+"*"*(x[i]//100)+"("+str(x[i])+")")

This is how the output appear so far

Masukkan jumlah bin antara 1-25(0 untuk berhenti): 3
  1-33 :*************************************************************************************(8529)
 34-67 :*************(1302)
68-100 :*************************************************************************(7321)

But i need to make it appear vertically

And how to make the output as "Maaf, hanya menerima angka 1-25"(Sorry, only number 1-25 allowed) if the user input alphabetical character? Thankyou.


Solution

With minimizing mods to your code.

import random

def print_words_vertical(lst):
    '''
        Print words in columns
        
        lst is a list of strings
        Each string is comma separated words
    '''
    words_list = [x.split(',') for x in lst]
    
    # Longest word sublist
    longest_word_list = max(words_list, key=len)
    
    # Longest individual word
    longest_word = max([word for sublist in words_list for word in sublist], key = len)
    widest_width = len(longest_word)

    # Vertical Print
    for index in range(len(longest_word_list), -1, -1):
        for sublist in words_list:
            if index < len(sublist):
                # This sublist has enough word to show at this index
                print(sublist[index].center(widest_width), end ='')
            else:
                # Insert space since we don't have a word for this column
                print(' '*widest_width, end = '')

        print()  # Newline between rows
    print()
        

Bins=1
while Bins!=0:
  Bins = input("Masukkan jumlah bin antara 1-25(0 untuk berhenti): ")
  Bins = int(Bins)                   # Preferable to use int() function to convert number to int since eval is unsafe
  if Bins==0:
    print("Done! Program selesai.")
    break
  elif Bins>1:
    x=[]
    c=random.randint(0,10000)
    for i in range(Bins):
      x.append(c)
      c=random.randint(0,10000-c)
    if sum(x)!=10000:x.append(10000-sum(x))
    else:x.append(0)
  elif Bins==1: x=[10000]
  if Bins<0 or Bins>25:
    print("Maaf, hanya menerima angka 1-25")
    continue
  tmp=-1

  # Capture bin information into a comma delimited string s
  lst = []
  for i in range(Bins):
    tmp=round(100/Bins*i)+1
    tmp1=round(100/Bins*(i+1))
    # Append bin information into string, with commas between major units
    lst.append((str(tmp)+"-"+str(tmp1)).rjust(6)+" "+","+"*,"*(x[i]//100)+"("+str(x[i])+")")
    
  
  # Vetrical print bins
  print_words_vertical(lst)

Example Run

 Masukkan jumlah bin antara 1-25(0 untuk berhenti): 10
                                                                      
                                    (7701)                            
                                      *                               
                                      *                               
                                      *                               
                                      *                               
                                      *                               
                                      *                               
                                      *           (7048)              
                                      *             *                 
                      (6886)          *             *                 
                        *             *             *                 
                        *             *             *                 
                        *             *             *                 
                        *             *             *                 
                        *             *             *                 
                        *             *             *                 
 (6151)                 *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *                 
   *                    *             *             *           (5104)
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *                    *             *             *             *   
   *    (3000)          *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *             *             *             *   
   *      *             *    (1914)   *             *             *   
   *      *             *      *      *             *             *   
   *      *             *      *      *             *             *   
   *      *             *      *      *             *             *   
   *      *             *      *      *             *             *   
   *      *             *      *      *             *             *   
   *      *             *      *      *             *    (1376)   *   
   *      *             *      *      *             *      *      *   
   *      *             *      *      *             *      *      *   
   *      *             *      *      *             *      *      *   
   *      *             *      *      *    (980)    *      *      *   
   *      *             *      *      *      *      *      *      *   
   *      *             *      *      *      *      *      *      *   
   *      *             *      *      *      *      *      *      *   
   *      *    (513)    *      *      *      *      *      *      *   
   *      *      *      *      *      *      *      *      *      *   
   *      *      *      *      *      *      *      *      *      *   
   *      *      *      *      *      *      *      *      *      *   
   *      *      *      *      *      *      *      *      *      *   
   *      *      *      *      *      *      *      *      *      *   
  1-10  11-20  21-30  31-40  41-50  51-60  61-70  71-80  81-90 91-100 


Answered By - DarrylG
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing