Reputation: 7
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.
Upvotes: 0
Views: 89
Reputation: 1607
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)
Upvotes: 0
Reputation: 1057
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)
Upvotes: 0
Reputation: 107
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)
Upvotes: 1