-1

To calculate the number of unique handshakes in a room, given the number of people. Where n is the number of people.

I am trying to calculate the number of handshakes people in a given room would exchange given the number of people, in python. This is written in Python 3.

people = input("How many people were there?")
no_handshakes = ((people - 1) *people)/2
print("No. of people: ", people)
print("No. of handshakes: ", no_handshakes)

When I run this code in the python shell, it generates this error.

Traceback (most recent call last):
  File "C:\Users\Tanaka\Desktop\Cloud\python101\peoplehandshakescalculator.py", line 2, in <module>
    no_handshakes = ((people - 1) *people)/2
TypeError: unsupported operand type(s) for -: 'str' and 'int'
Tanaka
  • 301
  • 2
  • 13

1 Answers1

0

The people var was being input manually as a string so it was necessary to convert it to integer/ number with

int()

and then the instructions would execute seamlessly as below.

    people = input("How many people were there?")
    no_handshakes = ((int(people)-1)*int(people))/2

    print("No. of people: ", people)
    print("No. of handshakes: ", no_handshakes)
Tanaka
  • 301
  • 2
  • 13
  • 3
    It'd be better to do `num_people = int(input("How many people were there?"))`, since leaving `people` as a string isn't that useful. – Blender Aug 28 '17 at 02:19