How to store value in a variable and return its primitive type and other information

Asked

Viewed 95 times

-3

I need to make a program that reads something on the keyboard and shows on the screen its primitive type and all possible information about it.

I’m still developing the code. But I’ve already noticed a bug and I don’t know how to fix it.

Follows my code:

n1 = float(input('Digite alguma coisa: '))

print('{} é', type(n1), 'além disso, ele é'.format(n1))

After "run" in the code, the place where the "n1" should be, continues to appear "{}".

  • Please try to better formulate your question. See these links on how to ask questions here https://pt.meta.stackoverflow.com/questions/8388/que-erro-eu-cometi-fazendo-minha-perguntar?cb=1 and here https://pt.meta.stackoverflow.com/questions/8089/guia-de-sobreviv%C3%aancia-do-sopt-vers%C3%a3o-curta? cb=1. For example: in the question title, put your question so that people already identify what you need. You also need to tag the language you are using. See other questions and try to follow the templates.

2 answers

2

When you do:

print('{} é', type(n1), 'além disso, ele é'.format(n1))

You’re passing 3 arguments to print, since they are separated by comma:

  • the string '{} é'
  • the result of type(n1)
  • the result of 'além disso, ele é'.format(n1)

They are 3 "independent" values, that is, each of them is passed separately, and print prints one by one.

That means that the method format is only applied to the string 'além disso, ele é', and how you don’t have the placeholder {} in this string, there is nothing to replace.

Anyway, an alternative to your case is to put everything in a single string and call format passing the values you want:

print('o valor da variável é {}, e seu tipo é {}'.format(n1, type(n1)))

Or, if you’re using Python >= 3.6, you can use f-string:

print(f'o valor da variável é {n1}, e seu tipo é {type(n1)}')

Note the "f" before the quotation marks, this indicates that it is an f-string and that I can put the figures to be printed inside the brackets.

0

You can put an f before the string in the print and n1 inside {}, so python will run what is inside

Ex:

n1 = float(input('Digite alguma coisa: '))

print(f'{n1} é', type(n1), 'além disso, ele é'.format(n1))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.