我有一段python代码,如果变量等于字符串' x'应该结束,但它没有,我不明白为什么。请有人解释一下
counter = 0
total_price = 0
biggest_price = 0
smallest_price = 10000000000
house_type = 0
while house_type != "x":
house_type = input("What is the house type? ")
if house_type != "x":
number_of_rooms = int(input("How many rooms does the house have? "))
age = int(input("How old is the house? "))
price = int(input("What is the houses price? "))
if price > biggest_price :
biggest_house_type = house_type
biggest_rooms = number_of_rooms
biggest_age = age
biggest_price = price
if price < smallest_price :
smallest_house_type = house_type
smallest_rooms = number_of_rooms
smallest_age = age
smallest_house_price = price
total_price = total_price + price
counter = counter + 1
print(biggest_house_type, biggest_rooms, biggest_age, biggest_price)
print(smallest_house_price, smallest_rooms, smallest_age, smallest_price)
print(total_price / counter)
有人可以解释为什么程序在按下X时不会结束,而只是给house_type值&#39; x&#39;
答案 0 :(得分:0)
有很多问题。我试着做一个最小的工作示例。
<?php
// Values received via ajax
$title = $_POST['title'];
$start = $_POST['start'];
$end = $_POST['end'];
$url = $_POST['url'];
// connection to the database
try {
$bdd = new PDO('mysql:host=localhost;dbname=fullcalendar', '....', '.......');
} catch(Exception $e) {
exit('Unable to connect to database.');
}
$sql = "INSERT INTO "
. "`evenement` "
. "SET "
. "`title` = :title, "
. "`start` = :start, "
. "`end` = :end, "
. "`url` = :url ";
$stmt = $pdo->prepare($sql);
$result = $stmt->execute(array(':title'=>$title, ':start'=>$start, ':end'=>$end, ':url'=>$url));
if (!$result) {
print_r($pdo->errorInfo());
}
?>
输出:
counter = 0
total_price = 0
biggest_price = 0
smallest_price = 10000000000
house_type = ''
while house_type != "x":
house_type = raw_input("What is the house type? ")
if house_type != "x":
number_of_rooms = int(raw_input("How many rooms does the house have? "))
age = int(raw_input("How old is the house? "))
price = int(raw_input("What is the houses price? "))
if price > biggest_price :
biggest_price = price
if price < smallest_price :
smallest_price = price
total_price = total_price + price
counter = counter + 1
print(biggest_price)
print(smallest_price)
print(total_price / counter)
注意强>:
如果您使用的是Python 2.x,请使用raw_input()。如果您使用的是Python 3.x,请使用input()