Hướng dẫn check positive integer python

Asked 7 years, 11 months ago

Viewed 99k times

I need to check whether what the user entered is positive. If it is not I need to print an error in the form of a msgbox.

number = input("Enter a number: ")
   ###################################

   try:
      val = int(number)
   except ValueError:
      print("That's not an int!")

The above code doesn't seem to be working.

Any ideas?

Hướng dẫn check positive integer python

jww

92.6k85 gold badges380 silver badges833 bronze badges

asked Oct 4, 2014 at 23:22

5

while True:
    number = input("Enter a number: ")
    try:
        val = int(number)
        if val < 0:  # if not a positive int print message and ask for input again
            print("Sorry, input must be a positive integer, try again")
            continue
        break
    except ValueError:
        print("That's not an int!")     
# else all is good, val is >=  0 and an integer
print(val)

answered Oct 4, 2014 at 23:32

Hướng dẫn check positive integer python

7

what you need is something like this:

goodinput = False
while not goodinput:
    try:
        number = int(input('Enter a number: '))
        if number > 0:
            goodinput = True
            print("that's a good number. Well done!")
        else:
            print("that's not a positive number. Try again: ")
    except ValueError:
        print("that's not an integer. Try again: ")

a while loop so code continues repeating until valid answer is given, and tests for the right input inside it.

answered Oct 4, 2014 at 23:37

W1ll1amvlW1ll1amvl

1,1592 gold badges15 silver badges30 bronze badges

0

Not the answer you're looking for? Browse other questions tagged python or ask your own question.

Viết chương trình Python để đếm số dương và số âm trong Tuple bằng cách sử dụng phạm vi vòng lặp for. Điều kiện if (if (posngTuple[i] > = 0)) kiểm tra xem mục Tuple lớn hơn hoặc bằng 0. Nếu Đúng, chúng tôi thêm một vào số Tuple Tích cực; nếu không (tNegativeCount = tNegativeCount + 1), hãy thêm một vào giá trị số Tuple Phủ định.

# Count Positive and Negative Numbers posngTuple = (3, -22, -44, 19, -99, -37, 4, 11, -89) print("Positive and Negative Tuple Items = ", posngTuple) tPositiveCount = tNegativeCount = 0 for i in range(len(posngTuple)): if (posngTuple[i] >= 0): tPositiveCount = tPositiveCount + 1 else: tNegativeCount = tNegativeCount + 1 print("The Count of Positive Numbers in posngTuple = ", tPositiveCount) print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)

Trong ví dụ Tích cực và Phủ định Python này, chúng tôi đã sử dụng vòng lặp for (cho pntup trong posngTuple) để lặp lại các giá trị tuple thực tế và kiểm tra xem chúng lớn hơn hay bằng 0.

# Count Positive and Negative Numbers posngTuple = (55, -99, -88, 0, -78, 22, 4, -66, 21, 33) print("Positive and Negative Tuple Items = ", posngTuple) tPositiveCount = tNegativeCount = 0 for pntup in posngTuple: if(pntup >= 0): tPositiveCount = tPositiveCount + 1 else: tNegativeCount = tNegativeCount + 1 print("The Count of Positive Numbers in posngTuple = ", tPositiveCount) print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)Positive and Negative Tuple Items = (55, -99, -88, 0, -78, 22, 4, -66, 21, 33) The Count of Positive Numbers in posngTuple = 6 The Count of Negative Numbers in posngTuple = 4

Chương trình Python để đếm Tích cực và Phủ định trong Tuple bằng cách sử dụng Vòng lặp While.

# Count of Tuple Positive and Negative Numbers posngTuple = (11, -22, -33, 44, 55, -66, -77, 0, -99) print("Positive and Negative Tuple Items = ", posngTuple) tPositiveCount = tNegativeCount = 0 i = 0 while (i < len(posngTuple)): if(posngTuple[i] >= 0): tPositiveCount = tPositiveCount + 1 else: tNegativeCount = tNegativeCount + 1 i = i + 1 print("The Count of Positive Numbers in posngTuple = ", tPositiveCount) print("The Count of Negative Numbers in posngTuple = ", tNegativeCount)Positive and Negative Tuple Items = (11, -22, -33, 44, 55, -66, -77, 0, -99) The Count of Positive Numbers in posngTuple = 4 The Count of Negative Numbers in posngTuple = 5

Trong ví dụ Python Tuple này, chúng tôi đã tạo một hàm trả về số lượng số Dương và Số âm.

# Count of Tuple Positive and Negative Numbers def CountOfPositiveNegativeNumbers(evodTuple): tPositiveCount = tNegativeCount = 0 for pntup in evodTuple: if(pntup >= 0): tPositiveCount = tPositiveCount + 1 else: tNegativeCount = tNegativeCount + 1 return tPositiveCount, tNegativeCount evodTuple = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) print("Positive and Negative Tuple Items = ", evodTuple) PositiveCount, NegativeCount = CountOfPositiveNegativeNumbers(evodTuple) print("The Count of Positive Numbers in evodTuple = ", PositiveCount) print("The Count of Negative Numbers in evodTuple = ", NegativeCount)Positive and Negative Tuple Items = (26, -77, -99, 75, 14, -56, 19, 81, -1, 33) The Count of Positive Numbers in evodTuple = 6 The Count of Negative Numbers in evodTuple = 4