I need help on the if and Else structure

Asked

Viewed 125 times

2

I created two 'if and Else' structures to make the program write to the user using the plural correctly and as you can see below, I created two variables within the structures to assign the plural that the program decides. Is there any way to assign values to two variables within a single structure? Do something like:

if m > 1: b = metros, c = centimetros? 

As I don’t know what value the user will provide, I decided to declare the variable as float, but as float are the real numbers, if the user type an integer number, the result will be . 0 at the end. For that not to happen, I would need to create another structure if and else?

I am using Python 3.7 and IDE Pycharm.

Make a Program that converts meters to centimeters.

m = float(input('Insira a medida em metros: '))

b = 'd'

if m > 1:
    b = 'metros'

else:
    b = 'metro'

if m > 1:
    d = 'equivalem'

else:
    d = 'equivale'


c = m * 100

print(f'{m} {b} {d} a {c} centimetros')  
  • See https://answall.com/q/101691/101

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the site.

1 answer

4

Can do yes:

if m > 1:
    b = 'metros'
    d = 'equivalem'
else:
    b = 'metro'
    d = 'equivale'

If you’re going to use words in sequence, then you don’t even have to keep two variables:

if m > 1:
    b = 'metros equivalem'
else:
    b = 'metro equivale'

I put in the Github for future reference.

I think it would be better to use more significant variable names.

Ever thought that one can type 0? What solution do you want to give for this? Will pluralize?

You can do this in a more modern and correct way, but I prefer not to introduce these things to you now.

Rounding has already been answered in How to "round" a float in Python?.

Browser other questions tagged

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