Home > Mobile >  Is it possible to add to a string based on user input python
Is it possible to add to a string based on user input python

Time:05-17

I am currently working on a program that takes in a user input, and depending on that user input the a string should change. I was wondering if there was a way I could alter the string once the user input has been received. The following is sample code.

title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter input('Subchapter: ')

title1 = '/title{}'.format(title)
subtitle1 = '/subtitle{}'.format(subtitle)
chapter1 = '/chapter{}'.format(chapter)
subchapter1 = '/subchapter{}'.format(subchapter)

output_txt = '**whatever follows depending on user input**'
print(output_txt)
  1. Input taken by user: Should be a number
  2. The input would then be formatted to its perspective string
  3. Based on the user input the string, output_txt, should be formatted accordingly

Scenario 1: User Input

Title: 4
Subtitle:
Chapter: 12
Subchapter: 1

output_txt should be

output_txt = '/title4/chapter12/subchapter1'

Scenario 2: User Input

Title: 9
Subtitle: 
Chapter: 2
Subchapter: 

output_txt should be

output_txt = '/title9/chapter2'

I have been doing using if elif but since there could be multiple combinations I do not think doing it that way is the most efficient.

Any help or tips in the right direction is greatly appreciated

CodePudding user response:

You could use an if-else condition while assigning string values to the variable

title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')

title1 = '/title{}'.format(title) if title else ''
subtitle1 = '/subtitle{}'.format(subtitle) if subtitle else ''
chapter1 = '/chapter{}'.format(chapter) if chapter else ''
subchapter1 = '/subchapter{}'.format(subchapter) if subchapter else ''

output_txt = title1 subtitle1 chapter1 subchapter1
print(output_txt)

CodePudding user response:

Let me introduce you to typer

typer will help you to create a CLI with python easily

for your code can be approached like this

import typer

# By defining the data type, we can set the input only a number
def main(title: int = None, subtitle: int = None, chapter: int = None, subchapter: int = None):
    title = f'/title{title}' if title else ''
    subtitle = f'/subtitle{subtitle}' if subtitle else ''
    chapter = f'/chapter{chapter}' if chapter else ''
    subchapter = f'/subchapter{subchapter}' if subchapter else ''

    output_txt = title subtitle chapter subchapter
    print(output_txt)


if __name__ == "__main__":
    typer.run(main)

And you just need to run it by adding the parameter for each one you need

python script_name.py --title 5 --chapter 2 --subchapter 7

CodePudding user response:

Just for completeness you might want to test for a number as well.

# Test

def test_for_int(testcase):
    try:
        int(testcase)
        return True
    except ValueError: # Strings
        return False
    except TypeError: # None
        return False

# Get the inputs

title = input('Title: ')
subtitle = input('Subtitle: ')
chapter = input('Chapter: ')
subchapter = input('Subchapter: ')

# Run the tests and build the string

output_txt = ''
if test_for_int(title):
    output_txt  = '/title{}'.format(title)
if test_for_int(subtitle):
    output_txt  = '/subtitle{}'.format(subtitle)
if test_for_int(chapter):
    output_txt  = '/chapter{}'.format(chapter)
if test_for_int(subchapter):
    output_txt  = '/subchapter{}'.format(subchapter)
    
print(output_txt)
  • Related