python if else语句使用演示的代码

2020-06-10  本文已影响0人  ahtia

如下的资料是关于python if else语句使用演示的代码,应该对小伙伴们有较大帮助。

#------------------------------------------------------------------------------

#          Name: if_conditional.py

#        Author: Kevin Harris

#  Last Modified: 02/13/04

#    Description: This Python script demonstrates how to use if-else

#                conditionals.

#------------------------------------------------------------------------------

# Our first example is a simple if/else statement

print( "Please enter a negative number" )

number = int( input( "> " ) )

if number >= 0:

    print( "That wasn't negative!" )

else:

    print( "Thank you!" )

# Our next example is a simple if/else if/else statement

print( "Which type of pet do you prefer?" )

print( " cats" )

print( " dogs" )

# Note how we use raw_input to get the input as a string

pet = input( "> " )

if pet == "cats":

    print( "You chose cats." )

elif pet == "dogs":

    print( "You chose dogs." )

else:

    print( "That's not one of the choices." )

input( 'nnPress Enter to exit...' )

上一篇 下一篇

猜你喜欢

热点阅读