如何在Python 3的if语句中使用Random Function?

时间:2017-12-31 20:22:06

标签: python python-3.6

这是我的简单抛硬币程序的代码,我是python的新手。

我没有收到任何错误,但程序不会打印我的“if”语句,它会直接从输入跳过输入。

我确信有更多的直觉方式来制作抛硬币计划,这只是我的想法。

有什么想法?

import random
print('--HEADS or TAILS--')
print('Welcome Players!')
print('[H=_Heads][T=_Tails]')
print('Please ENTER {H_or_T}')

input("Heads or Tails:")
rand = (random.randint(1,2))

if rand=='1':
    print("Heads Wins!")
elif rand=='2':
    print("Tails Wins!")

input("Press ENTER to Exit")

Here is a screenshot of my code.

2 个答案:

答案 0 :(得分:0)

random.randint(1,2)返回整数类型的值。 random.randint(a,b)文件清楚地说明了:

  

返回一个随机整数N ,使得< = N< = b。

但是在你的if中,你将它与字符串类型的值进行比较,因此你的条件语句失败了。

您需要将条件语句修改为:

#          v no quotes here
if rand == 1:
    print("Heads Wins!")
#            v no quotes here
elif rand == 2 :   # Better to simply use `else` 
    print("Tails Wins!")

答案 1 :(得分:0)

与已建议的名称randint一样,random.randint(1,2) 会返回1到2之间的整数(包括两者)。

如果您执行1 == '1',则intstr 进行比较,False始终为1。您应与'1进行比较,而不是elif'。

您也可以删除else,并使用2,因为我们知道1如果不是input("Heads or Tails:") rand = random.randint(1,2) # no brackets necessary if rand == 1: # use an integer, instead of a string: print("Heads Wins!") else: # else instead of elif print("Tails Wins!"),那么:{/ p>

--Functions
--(1)Tour Bill 
-- when enter a TourID it will genarate Tour Cost
--Multi statement table valued functions


CREATE FUNCTION dbo.Tour_Bill(@TourID int)
RETURNS @Bill TABLE 
(   TourID int,
    ItineraryID int,
    StartDate date,
    EndDate date,
    Duration int,
    Distance float,
    CustomerId varchar(30)
    Name varchar(180) NOT NULL,
    ContractNo varchar(12) NOT NULL,
    GuideID int,
    PaymentForGuide money,
    SpecielActivityCost money,
    VisitingPlaceTicketCost money,
    NumberOfPeople int,
    CostForMeal money,
    Accomadation varchar(100),
    TotalAccommodationCost money,
    TourPackegeCost money,
    TotalSpecielActivityCost money,
    TotalVisitingPlaceTicketCost money,
    GRAND_COST money
)  
AS 
BEGIN

INSERT INTO @Bill
         SELECT Tour.TourId,
                    Itinerary.ItineraryId,
                    StartDate,
                    EndDate,
                    DATEDIFF(day,StartDate,EndDate) AS Duration,
                    EstTravelDist,
                    CustomerId,
                    FirstName+' '+LastName AS FullName,
                    ContactNo,
                    Guide.IdNo,
                    CAST(500 * DATEDIFF(day,StartDate,EndDate) AS money) AS PaymentForGuide,
                    SpecialActivity.Cost AS SpecialActivityCost,
                    VisitingPlaces.Cost AS VisitingPlacesCost,
                    NumberOfPeople,
                    CAST(UnitPrice * DATEDIFF(day,StartDate,EndDate) * NumberOfPeople AS money) AS CostForMeal,
                    Location AS Accomadation,
                    CAST(UnitPrice * DATEDIFF(day,StartDate,EndDate) AS money) AS TotalAccommodationCost,
                    CAST(EstTravelDist * 40 AS money) AS TourPackegeCost, 
                    SpecialActivity.Cost * NumberOfPeople AS TotalSpecielActivityCost,
                    VisitingPlaces.Cost * NumberOfPeople AS TotalVisitingPlaceTicketCost,
                    CAST(EstTravelDist * 40 + SpecialActivity.Cost * NumberOfPeople + VisitingPlaces.Cost * NumberOfPeople + 500 * DATEDIFF(day,StartDate,EndDate) 
                    + UnitPrice * DATEDIFF(day,StartDate,EndDate) + UnitPrice * DATEDIFF(day,StartDate,EndDate) * NumberOfPeople AS money) AS GRAND_COST

    FROM (((((((((Itinerary
            INNER JOIN SpecialActivity ON
            Itinerary.ItineraryId = SpecialActivity.ItineraryId)
            INNER JOIN VisitingPlaces ON
            VisitingPlaces.ItineraryId = Itinerary.ItineraryId)
            INNER JOIN Tour ON
            Tour.TourId = Itinerary.TourId)
            INNER JOIN Guide ON
            Guide.TourId = Tour.TourId)
            INNER JOIN Vehicle ON
            Vehicle.TourId = Tour.TourId)
            INNER JOIN Accommodation ON
            Accommodation.TourId = Tour.TourId)
            INNER JOIN Participant ON
            Participant.TourId = Tour.TourId)
            INNER JOIN Person ON
            Person.IdNo = Guide.IdNo)
            INNER JOIN Contract ON
            Itinerary.ItineraryId = Contract.ItineraryId)
    WHERE Tour.TourId = 4
RETURN;
END

--How to Call the Function

SELECT * FROM dbo.Tour_Bill(101)

---These are the tables
create table Itinerary(
    ItineraryId int primary key identity,
    Description varchar(50)NOT NULL,
    TourId int,
    CONSTRAINT FK_Tour3 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId)  
);
--ALTER TABLE ADD COLUMN Distance
ALTER TABLE Itinerary
ADD EstTravelDist float NOT NULL

create table SpecialActivity(
    Activity varchar(100),
    ItineraryId int NOT NULL,
    Dates date ,
    PRIMARY KEY(Activity,ItineraryId,Dates),     
    CONSTRAINT FK_Dates3 FOREIGN KEY (Dates,ItineraryId) 
    REFERENCES Dates(Dates,ItineraryId),        
);
---ADD COLUMN Cost
ALTER TABLE SpecialActivity
ADD Cost money NOT NULL

create table VisitingPlaces(
    Place varchar(100),
    ItineraryId int,
    Dates date ,    
    PRIMARY KEY(Place,ItineraryId,Dates),    
    CONSTRAINT FK_Dates5 FOREIGN KEY (Dates,ItineraryId)
    REFERENCES Dates(Dates,ItineraryId),        
);
---ADD COLUMN Cost
ALTER TABLE VisitingPlaces
ADD Cost money NOT NULL

create table Person(
    IdNo varchar(30) primary key,
    FirstName varchar(50) NOT NULL ,
    LastName varchar (100) NOT NULL,
    Gender char(1) NOT NULL,
    Nationality varchar  (50) NOT NULL,
    Email varchar(100) UNIQUE,
    Fax varchar(12) ,
    PostBox varchar (6) NOT NULL,
    Street varchar (60) NOT NULL, 
    City varchar (60) NOT NULL,
    Province varchar (60) NOT NULL,
    Country varchar(100) NOT NULL    
);

create table Tour(
    TourId int PRIMARY KEY IDENTITY,
    ResavationDate date NOT NULL,
    StartDate date NOT NULL,
    EndDate date NOT NULL,
    NumberOfPeople int NOT NULL,
    TourStatus varchar(20) NOT NULL,
    CustomerId varchar(30),
    CONSTRAINT FK_Person3 Foreign key (CustomerId)
     REFERENCES Person(IdNo) 
 );


create table Guide(
    IdNo VARCHAR(30) PRIMARY KEY ,
    GovermentRegistrationNo varchar(10) NOT NULL UNIQUE,
    YearsOfExperence int NOT NULL,
    TourId int,
    CONSTRAINT FK_Tour2 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId),
    CONSTRAINT FK_Person4 FOREIGN KEY (IdNo) 
    REFERENCES Guide(IdNo)
 );

create table Participant(
    ParticipantId varchar(30) primary key,
    Name varchar(50) NOT NULL,
    ContactNo varchar(30) NOT NULL,
    PickupLocation varchar(50) NOT NULL,
    DropLocation varchar(50) NOT NULL,
    TourId int
    CONSTRAINT FK_Tour FOREIGN KEY (TourId) REFERENCES Tour(TourId) 
);

create table Accommodation(
    ContractNo int primary key ,
    Location varchar (50) NOT NULL,
    Class varchar(10),
    RentedRoomFacilitiesDescription varchar(1000),
    PvtRoomFacilitiesDescription varchar(1000),
    T1 bit,
    T2 bit,
    T3 bit,
    Dates date,
    ItineraryId int,
    TourId int,
    CONSTRAINT Fk_Dates FOREIGN KEY (Dates,ItineraryId) 
    REFERENCES Dates(Dates,ItineraryId),    
    CONSTRAINT FK_Tour4 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId),
);

create table Contract(
    ContractNo int primary key IDENTITY,
    PricingCriteria varchar(100),
    UnitPrice money,
    MiniPeople int,
    MaxPeople int,
    ContractType varchar(100),
    FinancialPenalty money,
    ItineraryId int NOT NULL,
    SupplierId int,
    CONSTRAINT FK_Itinerary2 FOREIGN KEY (ItineraryId)
    REFERENCES Itinerary(ItineraryId),
    CONSTRAINT FK_SupplierRegNo FOREIGN KEY (SupplierId) REFERENCES Supplier(SupplierRegNo)  
);