Python 2,无法获得正确的最大值和最小值

时间:2015-12-01 05:17:46

标签: python

我的任务:

编写一个程序,提示输入数字列表,最后打印出数字的最大值和最小值。 (并且不要假设使用最小和最大功能)

例如。如果我输入4,5,7,完成,它应该打印最大值为7,最小值打印为4.

Max = None
Min = None
while True:
    num = raw_input("Enter a number: ")
    if num == "done":
        break
    try:
        int(num)
    except:
        print "Invalid input"
        continue
    int(num)
    if Max is None or num > Max :
        maximum = num
    if Min is None or num < Min :
        minimum = num
print maximum, minimum

使用的数字:4,5,7,完成

结果:最大和最小

打印7和7

3 个答案:

答案 0 :(得分:2)

在初始化之后,你永远不会设置Min和Max。在每次循环中都是None,因此您将 /****************************View_Profile******************************/ package cl.click.ui.activities; import android.support.v7.app.AppCompatActivity; import java.util.Observable; import java.util.Observer; import cl.click.ApplicationClass; import cl.click.R; import cl.click.utility.modals.MUserData; public class View_Profile extends AppCompatActivity implements Observer { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile_view); initialize(); } private final void initialize() { MUserData.ProfileDetails profileDetails=((ApplicationClass)getApplicationContext()).getProfileDetails(); profileDetails.addObserver(this); //Error here on setting the observer } @Override public void update(Observable observable, Object data) { LogUtil.e("data is:-",data.toString()); } } /****************************ApplicationClass******************************/ package cl.click; import android.app.Application; import java.util.Observable; import java.util.Observer; import cl.click.utility.modals.MUserData; public class ApplicationClass extends Application { private MUserData.ProfileDetails profileDetails; @Override public void onCreate() { super.onCreate(); profileDetails=new MUserData().new ProfileDetails(); } public MUserData.ProfileDetails getProfileDetails() { return profileDetails; } public void setProfileDetails(MUserData.ProfileDetails profileDetails) { this.profileDetails = profileDetails; } } maximum设置为新输入的数字。

要解决这个问题,要么将Max更改为最大值,将Min更改为最小值,要么相反:将最大值更改为最大值,将最小值更改为最小值。

你也丢掉了minimum的结果,所以你的num变量总是一个字符串。这意味着算法正在比较字符串而不是数字。它本身不会破坏,但它可能不会给你预期的结果(例如,给定3和20作为输入,你的代码会说20小于3)

答案 1 :(得分:2)

您测试 Select maker from product where type in('PC','laptop') except select maker from product where type='laptop' Min,但设置并打印Maxminimum

另外,正如TigerhawkT3所说,虽然使用字符串是合法的,但你可能想用整数进行测试; maximum会丢弃结果(它不会更改int(num)),您可能需要num

答案 2 :(得分:1)

%reset import pandas as pd import numpy as np import matplotlib.pyplot as plt import sklearn.ensemble as sk from sklearn.cross_validation import train_test_split from sklearn.preprocessing import Imputer dataset=pd.read_csv('raw_data/data_set_nass_ges_person_2014.csv',sep=";"); ds=dataset ds2 = ds[ds["INJ_SEV"] <=4 ] #to clean useless variables from 5 to 9 , MAXSEV_IM train, temp = train_test_split(ds2, train_size = 0.8) #training set test, val = train_test_split(temp, test_size=0.5) #test set, validation set rfc = sk.RandomForestClassifier(n_estimators=500, oob_score=True) train_data = train[train.columns[1:-1]] #input train_truth = train["INJ_SEV"] #target train_data = Imputer().fit_transform(train_data) train_truth = Imputer().fit_transform(train_truth) #to solve the problem of 32bit vs 64bit model = rfc.fit(train_data, train_truth) # Here appears the problem int(num)强制转换为整数,然后将其抛弃。您需要保存对它的引用。

num