0

I would like to create a program that will input the enter size to the user and then enter type of individual triangle to select whether it is empty or no_empty, output 1 to 4 if empty and output 5 to 8 if no_empty.

My code is now this.

tri = int(input("enter size : "))
empthy = input("type of rectangle triangle : " )
choice = int(input("Choose the triangle you want to draw.\n1. triangle1 \n2. triangle2 \n3. triangle3 \n4. triangle4 \n5. triangle5 \n6. triangle6 \n7. triangle7 \n8. triangle8\n== >"))
print_triangle(tri, empthy, choice)

But an example of the code that I want to make,

enter size: 10

type of rectangle triangle : empthy

Choose the triangle you want to draw n1. triangle1 \n2. triangle2 \n3. triangle3 \n4. triangle4

or

enter size: 10

type of rectangle triangle : no_empthy

Choose the triangle you want to draw n5. triangle5 \n6. triangle6 \n7. triangle7 \n8. triangle8

My problem is If you select no_empty in ("type of vertical triangle"), it will output everything from 1 to 8.

2
  • And what issues did you encounter? Commented May 25, 2020 at 4:10
  • If you select no_empty in ("type of vertical triangle"), it will output everything from 1 to 8. Commented May 25, 2020 at 4:15

3 Answers 3

1

Welcome to Stackoverflow brother!!

triangle_size = int(input("Enter size : "))
filling = input("Do you want empty triangle or no_empty triangle: ")
if(filling=="empty"):
   choice = int(input("Choose the triangle you want to draw.\n1. triangle1 \n2. triangle2 \n3. triangle3 \n4. triangle4\n== >"))
elif(filling=="no_empty"):
   choice = int(input("Choose the triangle you want to draw.\n5. triangle5 \n6. triangle6 \n7. triangle7 \n8. triangle8\n== >"))
print_triangle(triangle_size, empty, choice)
Sign up to request clarification or add additional context in comments.

Comments

0

You must use if - else to decide between the triangles you want to show to user such as:

tri = int(input("enter size : "))
empty = input("type of rectangle triangle : " )
if empty=="empty":
   choice = int(input("Choose the triangle you want to draw.\n1. triangle1 \n2. triangle2 \n3. triangle3 \n4. triangle4\n== >"))
elif empty=="no_empty":
   choice = int(input("Choose the triangle you want to draw.\n5. triangle5 \n6. triangle6 \n7. triangle7 \n8. triangle8\n== >"))
print_triangle(tri, empty, choice)

Comments

0

It is hard to tell without seeing the code of print_triangle function why does it output everything from 1 to 8, but if you need to validate your input you could do something like this.

1) Declare a function that determines if the input is valid. E.g. if we want to check if the input is right type of integer, we do:

def validate_integer_input(s):
    return int(s)

def validate_exactly_2_int_input(s):
    parts = s.split()
    if len(parts) != 2:
        raise ValueError
    return list(map(int, parts))

Validator returns value or raises ValueError if something is not good with the data.

2) Then you write modified input function as follows:

def validated_input(validator):
    while True:
        try:
            return validator(input())
        except ValueError:
            pass

This function will keep trying receiving the input from user until correct data is entered.

3) Use it:

a = validated_input(validate_integer_input)
pairs = validated_input(validate_exactly_2_int_input)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.