python int( ) function

前端 未结 5 1772
逝去的感伤
逝去的感伤 2021-02-15 13:06

The code below shows error if a decimal (eg. 49.9) is sent to next variable. Can you please tell me why? Why does int() converts it into an in

5条回答
  •  闹比i
    闹比i (楼主)
    2021-02-15 13:38

    import random
    import time
    import sys
    while True:
        x=random.randint(1,100)
        print('''Guess my number--it's from 1 to 100.''')
        z=0
        while True:
            z=z+1
            xx=int(str(sys.stdin.readline()))
            if xx > x:
                print("Too High!")
            elif xx < x:
                print("Too Low!")
            elif xx==x:
                print("You Win!! You used %s guesses!"%(z))
                print()
                break
            else:
                break
    

    in this, I first string the number str(), which converts it into an inoperable number. Then, I int() integerize it, to make it an operable number. I just tested your problem on my IDLE GUI, and it said that 49.8 < 50.

提交回复
热议问题