-3

Let's take this code for example:

Women = (input("What's the number of women?"))

Men = (input("What's the number of men?"))

print("Percentage of Men: " + ((Men//(Men+Women))*100) + "\n Percentage of Women: " + ((Women//(Men+Women))*100))

I'm getting an error Class 'str' does not define '__floordiv__', so the '//' operator cannot be used on its instances.

How can I solve this?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
David5
  • 1
  • 1
  • 3
    `input` returns a `str` (string), so you cannot divide it by a number (or another `str`). You have to convert it to a number (e.g. `int`) first. Surely C++ has types, too? – Jan Christoph Terasa Mar 01 '21 at 06:38
  • 1
    Hi David, welcome to Stack Overflow. Please, make sure your code is properly formatted when posting. Here you can have a read on [how to format your code](https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks) – BiOS Mar 01 '21 at 07:40
  • And then, once you have integers to operate on and want to print them: [How can I concatenate str and int objects?](https://stackoverflow.com/questions/25675943/how-can-i-concatenate-str-and-int-objects) – Tomerikoo Mar 01 '21 at 10:37

7 Answers7

5
Men = int(input("What's the percentage of men"))
Women = int(input("What's the percentage of women"))
Men = int(((Men / (Men + Women)) * 100))
Women = int(((Women / (Men + Women)) * 100))

Use an f string

print(f"Percentage of men {Men} \n Percentage of Women {Women}")
Daweed
  • 1,419
  • 1
  • 9
  • 24
JACKDANIELS777
  • 339
  • 1
  • 14
0

Using int() to convert to number and f string. Normal division (/) returns a fractional number, whereas floor division (//) truncates the decimal part and returns the quotient.So we are using, / division operator.

women = int(input("What's the number of women?"))

men = int(input("What's the number of men?"))

print(f"Percentage of Men: {(men/(men+women))*100} \n Percentage of Women: {(women/(men+women))*100}")

0

You should try,

Women = int(input("What's the number of women?"))
Men = int(input("What's the number of men?"))
Total = Men+Women
print(f' Percentage of men: {(Men/Total)*100} ')
print(f' Percentage of Women : {(Women /Total)*100}')
SaGaR
  • 534
  • 4
  • 11
0

Men and Women are str variables (input function returns a string).

  • You should cast them to int after reading from stdin
    • Women = int(Women)
    • Men = int(Men)
  • You should cast to str before printing (or just use formatted string)
  • You are using the wrong equation to calculate the percentage
    • ((Men//(Men+Women))*100) always returns 0
      • Men < (Men+Women) => Men/(Men+Women) < 1
      • // operator returns the int part of division => 0
    • (Men/(Men+Women))*100 is the right expression (you can cast to int if you want)

The whole program:

Women = int((input("What's the number of women?")))
Men = int((input("What's the number of men?")))
print("Percentage of Men: " + str(((Men/(Men+Women))*100)) + "\nPercentage of Women: " + str(((Women/(Men+Women))*100)))
Husseinfo
  • 472
  • 2
  • 9
0

I usually using this format

print "This is man: {}, This is woman: {}".format(man, woman)
Michael MacAskill
  • 2,411
  • 1
  • 16
  • 28
wa007
  • 105
  • 8
0

You can use the int() function. You can learn more about it here. In this case, this would be the correct code:

women = int(input("What's the number of women?")) // using int function

men = int(input("What's the number of men?")) // using int function

print(f"Percentage of Men: {str((men/(men+women))*100)}\n
Percentage of Women: {str((women/(men+women))*100))}")
Henry
  • 1,311
  • 1
  • 9
  • 26
0

As most of the people answered already you need to convert string to desired output. So basically the input function takes any input as str. We should convert it to desired data type.

Another issue is for the percentage calculation you have used // which is floor division it will always round down the results causing your output always to be 0.(https://www.w3schools.com/python/trypython.asp?filename=demo_oper_floordiv)

One last point is if you are using python2 or python3. In python3 using simple division(/) will work fine with integers. But in python2 it will work like a // if both denominator and numerator are of type (int). Hence you might not get desired results.

Below code should work fine for both python2 and python3. Because instead of converting input to int we will be converting it to float. Which will work with / properly in both python2 and python3.

Women = float(input("What's the number of women?"))

Men = float(input("What's the number of men?"))

print("Percentage of Men: ", (Men / (Men + Women)) * 100)
print("Percentage of Women: ", (Women / (Men + Women)) * 100)
Ritwik G
  • 406
  • 2
  • 8