roberthsu2003 / __2024_07_01_Shilin_python__

士林python ai 初階
Apache License 2.0
13 stars 2 forks source link

請要求使用者,輸入一個整數,輸出是正整數,負整數或是0,還有是奇數還是偶數 #2

Open roberthsu2003 opened 3 months ago

roberthsu2003 commented 3 months ago
number = int(input("請輸入整數:"))

必需檢查輸入格式錯誤

輸入

輸出

heychi9487 commented 3 months ago
try:
    number = int(input("請輸入整數:"))
    if(number>0):
        print(f"{number}是正整數")
    if(number<0):
        print(f"{number}是負整數")
    if(number==0):
        print(f"{number}是0")
    if(number%2==1):
        print(f"{number}是奇數")
    if(number%2==0):
        print(f"{number}是偶數")
except Exception as error:
    print(error)
Marcoke999 commented 3 months ago

try:
    number=int(input("請輸入一個整數"))

    if number //2 ==0:
        print(f"輸入{number}為偶數")
    else:
        print(f"輸入{number}為質數")
    if number < 0:
        print(f"輸入{number}為負數")
    elif number > 0:
        print(f"輸入{number}為正數")
    else:
        print(f"輸入為0")
except Exception as error:
    print(error)```
Bowei0204 commented 3 months ago
try:
    n = int(input("請輸入一個整數"))
except:
    print("輸入格式錯誤")

else:
    if(n > 0):
        if(n % 2 == 0):
            print(f"""{n}是正整數
{n}是偶數""")
        else:
            print(f"""{n}是正整數
{n}是奇數""")

    elif(n < 0):
        if(n % 2 == 0):
            print(f"""{n}是負整數
{n}是偶數""")
        else:
            print(f"""{n}是負整數
{n}是奇數""") 

    else:
        print(f"""{n}為零
{n}是偶數""")          
Sherry-gfsm commented 3 months ago
try:
    num = int(input("請輸入整數:"))
except:
    print("輸入格式錯誤")
else:
    if num>0:
        print(f"{num}是正整數")
    if num<0:
        print(f"{num}是負整數")
    if num==0:
        print(f"{num}是0")
    if num % 2==0:
        print(f"{num}是偶數")
    else:
        print(f"{num}是奇數")
yi372 commented 3 months ago

try:
   number = int(input("請輸入整數:"))
   if number < 0 :
    print(f"{number}是負整數")
   if number > 0 :
    print(f"{number}是正整數")
   if number == 0 :
    print(f"{number}是0")
   if number % 2 == 1 :
    print(f"{number}是奇數")
   if number % 2 == 0 : 
    print(f"{number}是偶數")
except:
   print("輸入格式有誤")
Bergess0212 commented 3 months ago
try:
    number = int(input("請輸入整數:"))
    if number < 0:
        print(f"{number}為負數")
    if number == 0:
        print(f"{number}為0")
    if number > 0:
        print(f"{number}為正數")
    if number % 2 == 1:
        print(f"{number}為奇數")
    if number % 2 == 0:
        print(f"{number}為偶數")
except Exception as error:
    print(error)
pinganko commented 3 months ago
try:
    number = int(input("請輸入整數:"))
    if number > 0:
        print(f'{number}是正整數')
        if number < 0:
            print(f'{number}是負整數')
    else :
        print(f'{number}為0')
    if number %2 == 0:
        print(f'{number}為偶數')
    else :
        print(f'{number}為奇數')
except Exception as error:
    print(error)
sp11055422 commented 3 months ago

螢幕擷取畫面 2024-07-09 170633

Liangliang1231 commented 3 months ago
try:
    num=int(input("請輸入整數"))
    if num>0:
        print(f"{num}是正數")
    if num<0:
        print(f"{num}是負數")
    if num==0:
        print(f"{num}是零")

    if num%2==1:
        print((f"{num}是奇數"))
    if num%2==0:
        print((f"{num}是偶數"))
except Exception as error:
    print(error)          

螢幕擷取畫面 2024-07-09 233103

Ron091 commented 3 months ago

try:
number = int(input("請輸入一個整數")) if(number > 0): print(f"{number}為正整數")

if(number == 0):
    print(f"{number}為零")

if(number < 0):
    print(f"{number}為負整數")

if(number % 2 == 1):
    print(f"{number}為奇數")

if(number % 2 == 0):
    print(f"{number}為偶數")

except Exception as error: print(error) print("應用程式結束")