我只是在学习编码,所以我决定为自己创建一个项目,创建一个找到抛物线零的函数。我认为我遇到的问题实际上是打印出sqrt
。
这是我收到的错误:
File "C:/Users/someb/AppData/Local/Programs/Python/Python37-32/Quadratic Formula Solver revised.py", line 10, in find_zero
return float(-b) + "+-" + float(math.sqrt(discriminant)) + "/" + float(2 * a)
TypeError: unsupported operand type(s) for +: 'float' and 'str'
这是我的第五次修订版代码,尝试使用不同的方式,这本来应该显示两个不同的答案。
#Real Zero Finder QUadratic Formula
import math
def find_zero(a,b,c):
discriminant = (b ** 2 - 4 * a * c)
if discriminant < 0 :
return "No real Zeros"
elif discriminant == 0 :
return "Vertex is the Zero"
else:
#This is where the error is taking place
return float(-b) + "+-" + float(math.sqrt(discriminant)) + "/" + float(2 * a)
def disc(a,b,c):
return math.sqrt(b ** 2 - 4 * a * c)
答案 0 :(得分:2)
您收到此错误,因为Python不知道如何向浮点数添加字符串。您知道您正在尝试将浮点数连接到字符串,但是Python却没有。
在Python 3.6+中打印多个内容的最简单方法是使用格式化的字符串文字(f字符串):https://docs.python.org/3.6/reference/lexical_analysis.html#f-strings
将f
放在字符串之前,然后将想要显示的内容放在花括号{ }
内。
return f'{-b} +- {math.sqrt(discriminant)} / {2 * a}'
答案 1 :(得分:1)
如上所述,您不能添加字符串和浮点数。由于您似乎正在输出一条消息,因此我更愿意通过将浮点数转换为字符串来解决此问题,然后“ +”将连接这些字符串。这将返回一个字符串,这可能比返回多个值更有用。
return str(-b) + " +- " + str(math.sqrt(discriminant)) + " / " + str(2 * a)
我也取消了float()转换...我认为他们在这里没有做任何事情。
答案 2 :(得分:-2)
问题在这里回报:
package a.bogin.revmidterm.Models;
public class StringInfos {
private int id;
private String string1;
private String string2;
private String string3;
public StringInfos(String string1, String string2, String string3) {
this.string1 = string1;
this.string2 = string2;
this.string3 = string3;
}
public String getString1() {
return string1;
}
public void setString1(String string1) {
this.string1 = string1;
}
public String getString2() {
return string2;
}
public void setString2(String string2) {
this.string2 = string2;
}
public String getString3() {
return string3;
}
public void setString3(String string3) {
this.string3 = string3;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
您正在尝试将 package a.bogin.revmidterm;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import java.util.ArrayList;
import a.bogin.revmidterm.Adapter.StringsAdapter;
import a.bogin.revmidterm.DB.DBManager;
import a.bogin.revmidterm.Models.StringInfos;
public class MainActivity extends AppCompatActivity {
ArrayList<StringInfos> listOfAll;
StringsAdapter strAdapter;
RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayoutManager = new LinearLayoutManager(this);
.setLayoutManager(mLayoutManager);
RecyclerView myOwnRc = findViewById(R.id.rc);
myOwnRc.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL));
mLayoutManager = new LinearLayoutManager(this);
myOwnRc.setLayoutManager(mLayoutManager);
listOfAll = DBManager.getAllItems();
strAdapter = new StringsAdapter(listOfAll);
myOwnRc.setAdapter(strAdapter);
myOwnRc.setLayoutManager(new LinearLayoutManager(this));
myOwnRc.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id){
}
});
}
public void addStuff(View v){
Intent addNew = new Intent(this, AddItem.class);
startActivity(addNew);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inf = getMenuInflater();
inf.inflate(R.menu.itemsstuff, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.ea:
deleteAll();
return true;
case R.id.pref:
seePrefs();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void seePrefs(){
}
public void deleteAll(){
listOfAll.clear();
finish();
startActivity(getIntent());
}
}
连接到 myOwnRc.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View v, int pos, long id){
}
});
,而不要使用加号,而要使用逗号:
return float(-b) + "+-" + float(math.sqrt(discriminant)) + "/" + float(2 * a)